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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -13
app.py CHANGED
@@ -15,22 +15,25 @@ from transformers import pipeline
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 = {
22
- "q": place,
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,9 +46,9 @@ def get_current_weather(place: str) -> str:
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)
@@ -54,14 +57,14 @@ def get_current_time_in_timezone(timezone: str) -> str:
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}")
@@ -88,12 +91,19 @@ def document_qna_tool(pdf_path: str, question: str) -> str:
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()
@@ -118,7 +128,7 @@ agent = CodeAgent(
118
  get_current_weather,
119
  image_generation_tool,
120
  search_tool,
121
- document_qna_tool,
122
  final_answer
123
  ],
124
  max_steps=6,
 
15
  API_KEY = os.getenv("Weather_Token")
16
 
17
  # -------------------- TOOL 1: Get Weather --------------------
18
+ @Tool
19
  def get_current_weather(place: str) -> str:
 
 
 
 
 
 
 
20
  try:
21
+ url = "https://api.openweathermap.org/data/2.5/weather"
22
+ params = {
23
+ "q": place,
24
+ "appid": API_KEY,
25
+ "units": "metric"
26
+ }
27
+
28
  response = requests.get(url, params=params)
29
  data = response.json()
30
+
31
  if response.status_code == 200:
32
  weather_desc = data["weather"][0]["description"]
33
  temperature = data["main"]["temp"]
34
  humidity = data["main"]["humidity"]
35
  wind_speed = data["wind"]["speed"]
36
+
37
  return (
38
  f"Weather in {place}:\n"
39
  f"- Condition: {weather_desc}\n"
 
46
  except Exception as e:
47
  return f"Error fetching weather data for '{place}': {str(e)}"
48
 
 
49
 
50
  # -------------------- TOOL 2: Get Time --------------------
51
+ @Tool
52
  def get_current_time_in_timezone(timezone: str) -> str:
53
  try:
54
  tz = pytz.timezone(timezone)
 
57
  except Exception as e:
58
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
59
 
 
60
 
61
  # -------------------- TOOL 3: Document QnA --------------------
62
  embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
63
  qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
64
 
65
+ def document_qna_tool_fn(pdf_path: str, question: str) -> str:
66
+ import traceback
67
+
68
  try:
69
  print(f"[DEBUG] PDF Path: {pdf_path}")
70
  print(f"[DEBUG] Question: {question}")
 
91
 
92
  prompt = f"Context: {best_context}\nQuestion: {question}"
93
  answer = qa_pipeline(prompt, max_new_tokens=100)[0]['generated_text']
94
+
95
  return f"Answer: {answer.strip()}"
96
 
97
  except Exception as e:
98
  return f"[EXCEPTION] {type(e).__name__}: {str(e)}\n{traceback.format_exc()}"
99
 
100
+ # Wrap the function using Tool(...)
101
+ document_qna_tool = Tool(
102
+ name="document_qna_tool",
103
+ description="Answer questions about a PDF document.",
104
+ func=document_qna_tool_fn
105
+ )
106
+
107
 
108
  # -------------------- Other Components --------------------
109
  final_answer = FinalAnswerTool()
 
128
  get_current_weather,
129
  image_generation_tool,
130
  search_tool,
131
+ document_qna_tool, # Correctly wrapped tool
132
  final_answer
133
  ],
134
  max_steps=6,