rathil123 commited on
Commit
d30b786
·
verified ·
1 Parent(s): 2fe2468

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -76
app.py CHANGED
@@ -3,39 +3,48 @@ import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
  import PyPDF2
5
 
6
- # Load model and tokenizer
7
  model_name = "ibm-granite/granite-3.2-2b-instruct"
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
 
 
 
9
  model = AutoModelForCausalLM.from_pretrained(
10
  model_name,
11
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
12
- device_map="auto" if torch.cuda.is_available() else None
 
13
  )
14
 
15
  # Ensure pad token is set
16
  if tokenizer.pad_token is None:
17
  tokenizer.pad_token = tokenizer.eos_token
18
 
19
- # ---------- Core Functions ----------
20
- def generate_response(prompt, max_length=1024):
21
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
22
 
23
- if torch.cuda.is_available():
24
- inputs = {k: v.to(model.device) for k, v in inputs.items()}
25
-
26
- with torch.no_grad():
27
- outputs = model.generate(
28
- **inputs,
29
- max_length=max_length,
30
- temperature=0.7,
31
- do_sample=True,
32
- pad_token_id=tokenizer.eos_token_id
33
- )
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- # Slice out only the generated part
36
- response_ids = outputs[0][inputs["input_ids"].shape[-1]:]
37
- response = tokenizer.decode(response_ids, skip_special_tokens=True)
38
- return response.strip()
39
 
40
  def extract_text_from_pdf(pdf_file):
41
  if pdf_file is None:
@@ -44,70 +53,26 @@ def extract_text_from_pdf(pdf_file):
44
  pdf_reader = PyPDF2.PdfReader(pdf_file)
45
  text = ""
46
  for page in pdf_reader.pages:
47
- if page.extract_text():
48
- text += page.extract_text() + "\n"
49
- return text
 
50
  except Exception as e:
51
- return f"Error reading PDF: {str(e)}"
 
52
 
53
  def eco_tips_generator(problem_keywords):
 
 
54
  prompt = (
55
  f"Generate practical and actionable eco-friendly tips for sustainable living "
56
  f"related to: {problem_keywords}. Provide specific solutions and suggestions:"
57
  )
58
- return generate_response(prompt, max_length=800)
 
59
 
60
  def policy_summarization(pdf_file, policy_text):
61
  if pdf_file is not None:
62
  content = extract_text_from_pdf(pdf_file)
63
- summary_prompt = (
64
- f"Summarize the following policy document and extract the most important points, "
65
- f"key provisions, and implications:\n\n{content}"
66
- )
67
  else:
68
- summary_prompt = (
69
- f"Summarize the following policy document and extract the most important points, "
70
- f"key provisions, and implications:\n\n{policy_text}"
71
- )
72
-
73
- return generate_response(summary_prompt, max_length=1200)
74
-
75
- # ---------- Gradio Interface ----------
76
- with gr.Blocks() as app:
77
- gr.Markdown("# 🌍 Eco Assistant & Policy Analyzer")
78
-
79
- with gr.Tabs():
80
- with gr.TabItem("♻️ Eco Tips Generator"):
81
- with gr.Row():
82
- with gr.Column():
83
- keywords_input = gr.Textbox(
84
- label="Environmental Problem/Keywords",
85
- placeholder="e.g., plastic, solar, water waste, energy saving...",
86
- lines=3
87
- )
88
- generate_tips_btn = gr.Button("Generate Eco Tips")
89
-
90
- with gr.Column():
91
- tips_output = gr.Textbox(label="Sustainable Living Tips", lines=15)
92
-
93
- generate_tips_btn.click(eco_tips_generator, inputs=keywords_input, outputs=tips_output)
94
-
95
- with gr.TabItem("📑 Policy Summarization"):
96
- with gr.Row():
97
- with gr.Column():
98
- pdf_upload = gr.File(label="Upload Policy PDF", file_types=[".pdf"])
99
- policy_text_input = gr.Textbox(
100
- label="Or paste policy text here",
101
- placeholder="Paste policy document text...",
102
- lines=5
103
- )
104
- summarize_btn = gr.Button("Summarize Policy")
105
-
106
- with gr.Column():
107
- summary_output = gr.Textbox(label="Policy Summary & Key Points", lines=20)
108
-
109
- summarize_btn.click(policy_summarization, inputs=[pdf_upload, policy_text_input], outputs=summary_output)
110
-
111
- # Launch app
112
- if __name__ == "__main__":
113
- app.launch(share=True)
 
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
  import PyPDF2
5
 
6
+ # ----------------- Model Setup -----------------
7
  model_name = "ibm-granite/granite-3.2-2b-instruct"
8
+
9
+ # Some models require trust_remote_code
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
11
+
12
  model = AutoModelForCausalLM.from_pretrained(
13
  model_name,
14
  torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
15
+ device_map="auto" if torch.cuda.is_available() else None,
16
+ trust_remote_code=True
17
  )
18
 
19
  # Ensure pad token is set
20
  if tokenizer.pad_token is None:
21
  tokenizer.pad_token = tokenizer.eos_token
22
 
 
 
 
23
 
24
+ # ----------------- Core Functions -----------------
25
+ def generate_response(prompt, max_length=512):
26
+ try:
27
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=256)
28
+
29
+ if torch.cuda.is_available():
30
+ inputs = {k: v.to(model.device) for k, v in inputs.items()}
31
+
32
+ with torch.no_grad():
33
+ outputs = model.generate(
34
+ **inputs,
35
+ max_length=max_length,
36
+ temperature=0.7,
37
+ do_sample=True,
38
+ pad_token_id=tokenizer.eos_token_id
39
+ )
40
+
41
+ # Slice only the generated continuation
42
+ response_ids = outputs[0][inputs["input_ids"].shape[-1]:]
43
+ response = tokenizer.decode(response_ids, skip_special_tokens=True)
44
+ return response.strip()
45
+ except Exception as e:
46
+ return f"⚠️ Error generating response: {str(e)}"
47
 
 
 
 
 
48
 
49
  def extract_text_from_pdf(pdf_file):
50
  if pdf_file is None:
 
53
  pdf_reader = PyPDF2.PdfReader(pdf_file)
54
  text = ""
55
  for page in pdf_reader.pages:
56
+ page_text = page.extract_text()
57
+ if page_text:
58
+ text += page_text + "\n"
59
+ return text.strip()
60
  except Exception as e:
61
+ return f"⚠️ Error reading PDF: {str(e)}"
62
+
63
 
64
  def eco_tips_generator(problem_keywords):
65
+ if not problem_keywords.strip():
66
+ return "⚠️ Please enter some keywords."
67
  prompt = (
68
  f"Generate practical and actionable eco-friendly tips for sustainable living "
69
  f"related to: {problem_keywords}. Provide specific solutions and suggestions:"
70
  )
71
+ return generate_response(prompt, max_length=400)
72
+
73
 
74
  def policy_summarization(pdf_file, policy_text):
75
  if pdf_file is not None:
76
  content = extract_text_from_pdf(pdf_file)
 
 
 
 
77
  else:
78
+ content = polic