SamarthPujari commited on
Commit
05c6f6c
·
verified ·
1 Parent(s): f1ca9f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -65
app.py CHANGED
@@ -1,4 +1,5 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
 
2
  import datetime
3
  import requests
4
  import pytz
@@ -14,17 +15,7 @@ from transformers import pipeline
14
  API_KEY = os.getenv("Weather_Token")
15
 
16
  # -------------------- TOOL 1: Get Weather --------------------
17
- @tool
18
  def get_current_weather(place: str) -> str:
19
- """
20
- A tool that fetches the current weather of a particular place.
21
-
22
- Args:
23
- place (str): A string representing a valid place (e.g., 'London/Paris').
24
-
25
- Returns:
26
- str: Weather description including condition, temperature, humidity, and wind speed.
27
- """
28
  api_key = API_KEY
29
  url = "https://api.openweathermap.org/data/2.5/weather"
30
  params = {
@@ -32,17 +23,14 @@ def get_current_weather(place: str) -> str:
32
  "appid": api_key,
33
  "units": "metric"
34
  }
35
-
36
  try:
37
  response = requests.get(url, params=params)
38
  data = response.json()
39
-
40
  if response.status_code == 200:
41
  weather_desc = data["weather"][0]["description"]
42
  temperature = data["main"]["temp"]
43
  humidity = data["main"]["humidity"]
44
  wind_speed = data["wind"]["speed"]
45
-
46
  return (
47
  f"Weather in {place}:\n"
48
  f"- Condition: {weather_desc}\n"
@@ -55,19 +43,10 @@ def get_current_weather(place: str) -> str:
55
  except Exception as e:
56
  return f"Error fetching weather data for '{place}': {str(e)}"
57
 
 
58
 
59
  # -------------------- TOOL 2: Get Time --------------------
60
- @tool
61
  def get_current_time_in_timezone(timezone: str) -> str:
62
- """
63
- A tool that fetches the current local time in a specified timezone.
64
-
65
- Args:
66
- timezone (str): A string representing a valid timezone (e.g., 'America/New_York').
67
-
68
- Returns:
69
- str: The current local time formatted as a string.
70
- """
71
  try:
72
  tz = pytz.timezone(timezone)
73
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
@@ -75,30 +54,14 @@ def get_current_time_in_timezone(timezone: str) -> str:
75
  except Exception as e:
76
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
77
 
 
78
 
79
  # -------------------- TOOL 3: Document QnA --------------------
80
  embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
81
  qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
82
 
83
- from smolagents.tools import Tool
84
-
85
- @Tool(name="document_qna_tool", description="Answer questions about a PDF document.")
86
  def document_qna_tool(pdf_path: str, question: str) -> str:
87
-
88
- """
89
- A tool that answers natural language questions about a given PDF document.
90
-
91
- Args:
92
- pdf_path (str): Path to the local PDF file.
93
- question (str): Question about the content of the PDF.
94
-
95
- Returns:
96
- str: Answer to the question based on the content.
97
- """
98
  import os, fitz, traceback
99
- from sentence_transformers import SentenceTransformer, util
100
- from transformers import pipeline
101
-
102
  try:
103
  print(f"[DEBUG] PDF Path: {pdf_path}")
104
  print(f"[DEBUG] Question: {question}")
@@ -106,50 +69,32 @@ def document_qna_tool(pdf_path: str, question: str) -> str:
106
  if not os.path.exists(pdf_path):
107
  return f"[ERROR] File not found: {pdf_path}"
108
 
109
- print("[DEBUG] Opening PDF...")
110
- try:
111
- doc = fitz.open(pdf_path)
112
- except RuntimeError as e:
113
- return f"[ERROR] Could not open PDF. It may be corrupted or encrypted. Details: {str(e)}"
114
-
115
- text_chunks = []
116
- for page in doc:
117
- text = page.get_text()
118
- if text.strip():
119
- text_chunks.append(text)
120
  doc.close()
121
 
122
  if not text_chunks:
123
  return "[ERROR] No readable text in the PDF."
124
 
125
- print(f"[DEBUG] Extracted {len(text_chunks)} text chunks.")
126
- print(f"[DEBUG] First text chunk preview:\n{text_chunks[0][:300]}...")
127
-
128
  embeddings = embedding_model.encode(text_chunks, convert_to_tensor=True)
129
  question_embedding = embedding_model.encode(question, convert_to_tensor=True)
130
 
131
- print("[DEBUG] Performing semantic search...")
132
  scores = util.pytorch_cos_sim(question_embedding, embeddings)[0]
133
-
134
- print(f"[DEBUG] Similarity scores: {scores}")
135
-
136
  if scores.shape[0] == 0:
137
  return "[ERROR] No semantic matches found in PDF text."
138
 
139
  best_match_idx = scores.argmax().item()
140
  best_context = text_chunks[best_match_idx]
141
 
142
- print(f"[DEBUG] Best context preview:\n{best_context[:300]}...")
143
-
144
  prompt = f"Context: {best_context}\nQuestion: {question}"
145
- print("[DEBUG] Calling QA model...")
146
  answer = qa_pipeline(prompt, max_new_tokens=100)[0]['generated_text']
147
-
148
  return f"Answer: {answer.strip()}"
149
 
150
  except Exception as e:
151
  return f"[EXCEPTION] {type(e).__name__}: {str(e)}\n{traceback.format_exc()}"
152
 
 
 
153
  # -------------------- Other Components --------------------
154
  final_answer = FinalAnswerTool()
155
  search_tool = DuckDuckGoSearchTool()
@@ -173,7 +118,7 @@ agent = CodeAgent(
173
  get_current_weather,
174
  image_generation_tool,
175
  search_tool,
176
- document_qna_tool, # ← New Tool Added
177
  final_answer
178
  ],
179
  max_steps=6,
@@ -184,8 +129,9 @@ agent = CodeAgent(
184
  description=None,
185
  prompt_templates=prompt_templates
186
  )
 
187
  print("[DEBUG] Registered Tools:")
188
  for t in agent.tools:
189
  print(f" - {getattr(t, 'name', str(t))}")
190
 
191
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool
2
+ from smolagents.tools import Tool
3
  import datetime
4
  import requests
5
  import pytz
 
15
  API_KEY = os.getenv("Weather_Token")
16
 
17
  # -------------------- TOOL 1: Get Weather --------------------
 
18
  def get_current_weather(place: str) -> str:
 
 
 
 
 
 
 
 
 
19
  api_key = API_KEY
20
  url = "https://api.openweathermap.org/data/2.5/weather"
21
  params = {
 
23
  "appid": api_key,
24
  "units": "metric"
25
  }
 
26
  try:
27
  response = requests.get(url, params=params)
28
  data = response.json()
 
29
  if response.status_code == 200:
30
  weather_desc = data["weather"][0]["description"]
31
  temperature = data["main"]["temp"]
32
  humidity = data["main"]["humidity"]
33
  wind_speed = data["wind"]["speed"]
 
34
  return (
35
  f"Weather in {place}:\n"
36
  f"- Condition: {weather_desc}\n"
 
43
  except Exception as e:
44
  return f"Error fetching weather data for '{place}': {str(e)}"
45
 
46
+ get_current_weather = Tool(name="get_current_weather", description="Fetch current weather for a given place")(get_current_weather)
47
 
48
  # -------------------- TOOL 2: Get Time --------------------
 
49
  def get_current_time_in_timezone(timezone: str) -> str:
 
 
 
 
 
 
 
 
 
50
  try:
51
  tz = pytz.timezone(timezone)
52
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
 
54
  except Exception as e:
55
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
56
 
57
+ get_current_time_in_timezone = Tool(name="get_current_time_in_timezone", description="Fetch local time for a given timezone")(get_current_time_in_timezone)
58
 
59
  # -------------------- TOOL 3: Document QnA --------------------
60
  embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
61
  qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
62
 
 
 
 
63
  def document_qna_tool(pdf_path: str, question: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
64
  import os, fitz, traceback
 
 
 
65
  try:
66
  print(f"[DEBUG] PDF Path: {pdf_path}")
67
  print(f"[DEBUG] Question: {question}")
 
69
  if not os.path.exists(pdf_path):
70
  return f"[ERROR] File not found: {pdf_path}"
71
 
72
+ doc = fitz.open(pdf_path)
73
+ text_chunks = [page.get_text() for page in doc if page.get_text().strip()]
 
 
 
 
 
 
 
 
 
74
  doc.close()
75
 
76
  if not text_chunks:
77
  return "[ERROR] No readable text in the PDF."
78
 
 
 
 
79
  embeddings = embedding_model.encode(text_chunks, convert_to_tensor=True)
80
  question_embedding = embedding_model.encode(question, convert_to_tensor=True)
81
 
 
82
  scores = util.pytorch_cos_sim(question_embedding, embeddings)[0]
 
 
 
83
  if scores.shape[0] == 0:
84
  return "[ERROR] No semantic matches found in PDF text."
85
 
86
  best_match_idx = scores.argmax().item()
87
  best_context = text_chunks[best_match_idx]
88
 
 
 
89
  prompt = f"Context: {best_context}\nQuestion: {question}"
 
90
  answer = qa_pipeline(prompt, max_new_tokens=100)[0]['generated_text']
 
91
  return f"Answer: {answer.strip()}"
92
 
93
  except Exception as e:
94
  return f"[EXCEPTION] {type(e).__name__}: {str(e)}\n{traceback.format_exc()}"
95
 
96
+ document_qna_tool = Tool(name="document_qna_tool", description="Answer questions about a PDF document")(document_qna_tool)
97
+
98
  # -------------------- Other Components --------------------
99
  final_answer = FinalAnswerTool()
100
  search_tool = DuckDuckGoSearchTool()
 
118
  get_current_weather,
119
  image_generation_tool,
120
  search_tool,
121
+ document_qna_tool,
122
  final_answer
123
  ],
124
  max_steps=6,
 
129
  description=None,
130
  prompt_templates=prompt_templates
131
  )
132
+
133
  print("[DEBUG] Registered Tools:")
134
  for t in agent.tools:
135
  print(f" - {getattr(t, 'name', str(t))}")
136
 
137
+ GradioUI(agent).launch()