Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,14 +16,22 @@ API_KEY = os.getenv("Weather_Token")
|
|
| 16 |
# -------------------- TOOL 1: Get Weather --------------------
|
| 17 |
@tool
|
| 18 |
def get_current_weather(place: str) -> str:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
| 27 |
response = requests.get(url, params=params)
|
| 28 |
data = response.json()
|
| 29 |
|
|
@@ -49,6 +57,13 @@ def get_current_weather(place: str) -> str:
|
|
| 49 |
# -------------------- TOOL 2: Get Time --------------------
|
| 50 |
@tool
|
| 51 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
tz = pytz.timezone(timezone)
|
| 54 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
@@ -63,7 +78,19 @@ qa_pipeline = pipeline("text2text-generation", model="google/flan-t5-base")
|
|
| 63 |
|
| 64 |
@tool
|
| 65 |
def document_qna_tool(pdf_path: str, question: str) -> str:
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
try:
|
| 69 |
print(f"[DEBUG] PDF Path: {pdf_path}")
|
|
@@ -72,24 +99,43 @@ def document_qna_tool(pdf_path: str, question: str) -> str:
|
|
| 72 |
if not os.path.exists(pdf_path):
|
| 73 |
return f"[ERROR] File not found: {pdf_path}"
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
doc.close()
|
| 78 |
|
| 79 |
if not text_chunks:
|
| 80 |
return "[ERROR] No readable text in the PDF."
|
| 81 |
|
|
|
|
|
|
|
|
|
|
| 82 |
embeddings = embedding_model.encode(text_chunks, convert_to_tensor=True)
|
| 83 |
question_embedding = embedding_model.encode(question, convert_to_tensor=True)
|
| 84 |
|
|
|
|
| 85 |
scores = util.pytorch_cos_sim(question_embedding, embeddings)[0]
|
|
|
|
|
|
|
|
|
|
| 86 |
if scores.shape[0] == 0:
|
| 87 |
return "[ERROR] No semantic matches found in PDF text."
|
| 88 |
|
| 89 |
best_match_idx = scores.argmax().item()
|
| 90 |
best_context = text_chunks[best_match_idx]
|
| 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()}"
|
|
@@ -120,7 +166,7 @@ agent = CodeAgent(
|
|
| 120 |
get_current_weather,
|
| 121 |
image_generation_tool,
|
| 122 |
search_tool,
|
| 123 |
-
document_qna_tool,
|
| 124 |
final_answer
|
| 125 |
],
|
| 126 |
max_steps=6,
|
|
@@ -131,9 +177,8 @@ agent = CodeAgent(
|
|
| 131 |
description=None,
|
| 132 |
prompt_templates=prompt_templates
|
| 133 |
)
|
| 134 |
-
|
| 135 |
print("[DEBUG] Registered Tools:")
|
| 136 |
for t in agent.tools:
|
| 137 |
print(f" - {getattr(t, 'name', str(t))}")
|
| 138 |
|
| 139 |
-
GradioUI(agent).launch()
|
|
|
|
| 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 |
+
Args:
|
| 22 |
+
place (str): A string representing a valid place (e.g., 'London/Paris').
|
| 23 |
+
Returns:
|
| 24 |
+
str: Weather description including condition, temperature, humidity, and wind speed.
|
| 25 |
+
"""
|
| 26 |
+
api_key = API_KEY
|
| 27 |
+
url = "https://api.openweathermap.org/data/2.5/weather"
|
| 28 |
+
params = {
|
| 29 |
+
"q": place,
|
| 30 |
+
"appid": api_key,
|
| 31 |
+
"units": "metric"
|
| 32 |
+
}
|
| 33 |
|
| 34 |
+
try:
|
| 35 |
response = requests.get(url, params=params)
|
| 36 |
data = response.json()
|
| 37 |
|
|
|
|
| 57 |
# -------------------- TOOL 2: Get Time --------------------
|
| 58 |
@tool
|
| 59 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 60 |
+
"""
|
| 61 |
+
A tool that fetches the current local time in a specified timezone.
|
| 62 |
+
Args:
|
| 63 |
+
timezone (str): A string representing a valid timezone (e.g., 'America/New_York').
|
| 64 |
+
Returns:
|
| 65 |
+
str: The current local time formatted as a string.
|
| 66 |
+
"""
|
| 67 |
try:
|
| 68 |
tz = pytz.timezone(timezone)
|
| 69 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
| 78 |
|
| 79 |
@tool
|
| 80 |
def document_qna_tool(pdf_path: str, question: str) -> str:
|
| 81 |
+
"""
|
| 82 |
+
A tool that answers natural language questions about a given PDF document.
|
| 83 |
+
|
| 84 |
+
Args:
|
| 85 |
+
pdf_path (str): Path to the local PDF file.
|
| 86 |
+
question (str): Question about the content of the PDF.
|
| 87 |
+
|
| 88 |
+
Returns:
|
| 89 |
+
str: Answer to the question based on the content.
|
| 90 |
+
"""
|
| 91 |
+
import os, fitz, traceback
|
| 92 |
+
from sentence_transformers import SentenceTransformer, util
|
| 93 |
+
from transformers import pipeline
|
| 94 |
|
| 95 |
try:
|
| 96 |
print(f"[DEBUG] PDF Path: {pdf_path}")
|
|
|
|
| 99 |
if not os.path.exists(pdf_path):
|
| 100 |
return f"[ERROR] File not found: {pdf_path}"
|
| 101 |
|
| 102 |
+
print("[DEBUG] Opening PDF...")
|
| 103 |
+
try:
|
| 104 |
+
doc = fitz.open(pdf_path)
|
| 105 |
+
except RuntimeError as e:
|
| 106 |
+
return f"[ERROR] Could not open PDF. It may be corrupted or encrypted. Details: {str(e)}"
|
| 107 |
+
|
| 108 |
+
text_chunks = []
|
| 109 |
+
for page in doc:
|
| 110 |
+
text = page.get_text()
|
| 111 |
+
if text.strip():
|
| 112 |
+
text_chunks.append(text)
|
| 113 |
doc.close()
|
| 114 |
|
| 115 |
if not text_chunks:
|
| 116 |
return "[ERROR] No readable text in the PDF."
|
| 117 |
|
| 118 |
+
print(f"[DEBUG] Extracted {len(text_chunks)} text chunks.")
|
| 119 |
+
print(f"[DEBUG] First text chunk preview:\n{text_chunks[0][:300]}...")
|
| 120 |
+
|
| 121 |
embeddings = embedding_model.encode(text_chunks, convert_to_tensor=True)
|
| 122 |
question_embedding = embedding_model.encode(question, convert_to_tensor=True)
|
| 123 |
|
| 124 |
+
print("[DEBUG] Performing semantic search...")
|
| 125 |
scores = util.pytorch_cos_sim(question_embedding, embeddings)[0]
|
| 126 |
+
|
| 127 |
+
print(f"[DEBUG] Similarity scores: {scores}")
|
| 128 |
+
|
| 129 |
if scores.shape[0] == 0:
|
| 130 |
return "[ERROR] No semantic matches found in PDF text."
|
| 131 |
|
| 132 |
best_match_idx = scores.argmax().item()
|
| 133 |
best_context = text_chunks[best_match_idx]
|
| 134 |
|
| 135 |
+
print(f"[DEBUG] Best context preview:\n{best_context[:300]}...")
|
| 136 |
+
|
| 137 |
prompt = f"Context: {best_context}\nQuestion: {question}"
|
| 138 |
+
print("[DEBUG] Calling QA model...")
|
| 139 |
answer = qa_pipeline(prompt, max_new_tokens=100)[0]['generated_text']
|
| 140 |
|
| 141 |
return f"Answer: {answer.strip()}"
|
|
|
|
| 166 |
get_current_weather,
|
| 167 |
image_generation_tool,
|
| 168 |
search_tool,
|
| 169 |
+
document_qna_tool, # ← New Tool Added
|
| 170 |
final_answer
|
| 171 |
],
|
| 172 |
max_steps=6,
|
|
|
|
| 177 |
description=None,
|
| 178 |
prompt_templates=prompt_templates
|
| 179 |
)
|
|
|
|
| 180 |
print("[DEBUG] Registered Tools:")
|
| 181 |
for t in agent.tools:
|
| 182 |
print(f" - {getattr(t, 'name', str(t))}")
|
| 183 |
|
| 184 |
+
GradioUI(agent).launch()
|