SamarthPujari commited on
Commit
2130f7e
·
verified ·
1 Parent(s): 1160e0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -21
app.py CHANGED
@@ -1,33 +1,48 @@
 
1
  import datetime
2
- import os
3
- import pytz
4
  import requests
 
5
  import yaml
 
 
 
6
  import fitz # PyMuPDF
7
  from sentence_transformers import SentenceTransformer, util
8
  from transformers import pipeline
9
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
10
- from tools.final_answer import FinalAnswerTool
11
- from Gradio_UI import GradioUI # Import your UI class
12
 
13
  # API Key for weather
14
  API_KEY = os.getenv("Weather_Token")
15
 
16
- # -------------------- TOOLS --------------------
17
-
18
  @tool
19
  def get_current_weather(place: str) -> str:
 
 
 
 
 
 
 
 
 
20
  api_key = API_KEY
21
  url = "https://api.openweathermap.org/data/2.5/weather"
22
- params = {"q": place, "appid": api_key, "units": "metric"}
 
 
 
 
 
23
  try:
24
  response = requests.get(url, params=params)
25
  data = response.json()
 
26
  if response.status_code == 200:
27
  weather_desc = data["weather"][0]["description"]
28
  temperature = data["main"]["temp"]
29
  humidity = data["main"]["humidity"]
30
  wind_speed = data["wind"]["speed"]
 
31
  return (
32
  f"Weather in {place}:\n"
33
  f"- Condition: {weather_desc}\n"
@@ -41,8 +56,18 @@ def get_current_weather(place: str) -> str:
41
  return f"Error fetching weather data for '{place}': {str(e)}"
42
 
43
 
 
44
  @tool
45
  def get_current_time_in_timezone(timezone: str) -> str:
 
 
 
 
 
 
 
 
 
46
  try:
47
  tz = pytz.timezone(timezone)
48
  local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
@@ -51,34 +76,52 @@ def get_current_time_in_timezone(timezone: str) -> str:
51
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
52
 
53
 
 
54
  embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
55
  qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
56
 
57
  @tool
58
  def document_qna_tool(pdf_path: str, question: str) -> str:
 
 
 
 
 
 
 
 
 
 
59
  try:
 
60
  doc = fitz.open(pdf_path)
61
- text_chunks = [page.get_text() for page in doc if page.get_text().strip()]
 
 
 
 
62
  doc.close()
63
 
64
  if not text_chunks:
65
  return "No text found in the PDF."
66
 
 
67
  embeddings = embedding_model.encode(text_chunks, convert_to_tensor=True)
68
  question_embedding = embedding_model.encode(question, convert_to_tensor=True)
69
  scores = util.pytorch_cos_sim(question_embedding, embeddings)[0]
70
  best_match_idx = scores.argmax()
71
  best_context = text_chunks[best_match_idx]
72
 
 
73
  prompt = f"Context: {best_context}\nQuestion: {question}"
74
  answer = qa_pipeline(prompt, max_new_tokens=100)[0]['generated_text']
75
  return f"Answer: {answer.strip()}"
 
76
  except Exception as e:
77
  return f"Error processing document QnA: {str(e)}"
78
 
79
 
80
- # -------------------- SETUP AGENT --------------------
81
-
82
  final_answer = FinalAnswerTool()
83
  search_tool = DuckDuckGoSearchTool()
84
 
@@ -101,20 +144,16 @@ agent = CodeAgent(
101
  get_current_weather,
102
  image_generation_tool,
103
  search_tool,
104
- document_qna_tool,
105
  final_answer
106
  ],
107
  max_steps=6,
108
  verbosity_level=1,
 
 
 
 
109
  prompt_templates=prompt_templates
110
  )
111
 
112
- # -------------------- LAUNCH UI --------------------
113
-
114
- # Optional: define an upload folder if you want file uploads saved
115
- upload_folder = "uploaded_files"
116
- if not os.path.exists(upload_folder):
117
- os.makedirs(upload_folder)
118
-
119
- ui = GradioUI(agent=agent, file_upload_folder=upload_folder)
120
- ui.launch(share=True)
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
  import datetime
 
 
3
  import requests
4
+ import pytz
5
  import yaml
6
+ import os
7
+ from tools.final_answer import FinalAnswerTool
8
+ from Gradio_UI import GradioUI
9
  import fitz # PyMuPDF
10
  from sentence_transformers import SentenceTransformer, util
11
  from transformers import pipeline
 
 
 
12
 
13
  # API Key for weather
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 = {
31
+ "q": place,
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"
 
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")
 
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
  @tool
84
  def document_qna_tool(pdf_path: str, question: str) -> str:
85
+ """
86
+ A tool for answering questions based on the content of a PDF document.
87
+
88
+ Args:
89
+ pdf_path (str): Path to the local PDF file.
90
+ question (str): A natural language question to ask about the PDF content.
91
+
92
+ Returns:
93
+ str: Answer to the question based on the PDF's content.
94
+ """
95
  try:
96
+ # Step 1: Extract text from PDF
97
  doc = fitz.open(pdf_path)
98
+ text_chunks = []
99
+ for page in doc:
100
+ text = page.get_text()
101
+ if text.strip():
102
+ text_chunks.append(text)
103
  doc.close()
104
 
105
  if not text_chunks:
106
  return "No text found in the PDF."
107
 
108
+ # Step 2: Semantic search
109
  embeddings = embedding_model.encode(text_chunks, convert_to_tensor=True)
110
  question_embedding = embedding_model.encode(question, convert_to_tensor=True)
111
  scores = util.pytorch_cos_sim(question_embedding, embeddings)[0]
112
  best_match_idx = scores.argmax()
113
  best_context = text_chunks[best_match_idx]
114
 
115
+ # Step 3: Answer question
116
  prompt = f"Context: {best_context}\nQuestion: {question}"
117
  answer = qa_pipeline(prompt, max_new_tokens=100)[0]['generated_text']
118
  return f"Answer: {answer.strip()}"
119
+
120
  except Exception as e:
121
  return f"Error processing document QnA: {str(e)}"
122
 
123
 
124
+ # -------------------- Other Components --------------------
 
125
  final_answer = FinalAnswerTool()
126
  search_tool = DuckDuckGoSearchTool()
127
 
 
144
  get_current_weather,
145
  image_generation_tool,
146
  search_tool,
147
+ document_qna_tool, # ← New Tool Added
148
  final_answer
149
  ],
150
  max_steps=6,
151
  verbosity_level=1,
152
+ grammar=None,
153
+ planning_interval=None,
154
+ name=None,
155
+ description=None,
156
  prompt_templates=prompt_templates
157
  )
158
 
159
+ GradioUI(agent).launch()