Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,8 @@ import gradio as gr
|
|
| 7 |
# CONFIG
|
| 8 |
# ===============================
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Hugging Face username
|
| 14 |
-
HF_USERNAME = "jatinror" # your HF username
|
| 15 |
-
|
| 16 |
-
# Your Space ID (updated)
|
| 17 |
SPACE_ID = "jatinror/Final_Assignment"
|
| 18 |
AGENT_CODE_URL = f"https://huggingface.co/spaces/{SPACE_ID}/tree/main"
|
| 19 |
|
|
@@ -54,7 +49,7 @@ def solve_question(question, task_id):
|
|
| 54 |
context = ""
|
| 55 |
if file_path and os.path.exists(file_path):
|
| 56 |
try:
|
| 57 |
-
with open(file_path, "r", errors="ignore") as f:
|
| 58 |
context = f.read()
|
| 59 |
except:
|
| 60 |
context = ""
|
|
@@ -63,16 +58,34 @@ def solve_question(question, task_id):
|
|
| 63 |
return extract_answer(context)
|
| 64 |
|
| 65 |
# ===============================
|
| 66 |
-
# ANSWER EXTRACTION
|
| 67 |
# ===============================
|
| 68 |
|
| 69 |
def extract_answer(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
numbers = re.findall(r"\b\d+(?:\.\d+)?\b", text)
|
| 72 |
if numbers:
|
| 73 |
return numbers[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
words = text.split()
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# ===============================
|
| 78 |
# FETCH QUESTIONS
|
|
@@ -95,7 +108,7 @@ def submit_answers(answers):
|
|
| 95 |
}
|
| 96 |
response = requests.post(f"{BASE_URL}/submit", json=payload)
|
| 97 |
print("Server response:", response.text)
|
| 98 |
-
return response.text # Return response to output box
|
| 99 |
|
| 100 |
# ===============================
|
| 101 |
# MAIN PIPELINE
|
|
@@ -116,7 +129,7 @@ def run_agent():
|
|
| 116 |
"submitted_answer": result.strip()
|
| 117 |
})
|
| 118 |
server_response = submit_answers(answers)
|
| 119 |
-
return server_response
|
| 120 |
|
| 121 |
# ===============================
|
| 122 |
# GRADIO BLOCKS UI
|
|
|
|
| 7 |
# CONFIG
|
| 8 |
# ===============================
|
| 9 |
|
| 10 |
+
BASE_URL = "https://agents-course-unit4-scoring.hf.space" # GAIA API URL
|
| 11 |
+
HF_USERNAME = "jatinror" # your Hugging Face username
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
SPACE_ID = "jatinror/Final_Assignment"
|
| 13 |
AGENT_CODE_URL = f"https://huggingface.co/spaces/{SPACE_ID}/tree/main"
|
| 14 |
|
|
|
|
| 49 |
context = ""
|
| 50 |
if file_path and os.path.exists(file_path):
|
| 51 |
try:
|
| 52 |
+
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
| 53 |
context = f.read()
|
| 54 |
except:
|
| 55 |
context = ""
|
|
|
|
| 58 |
return extract_answer(context)
|
| 59 |
|
| 60 |
# ===============================
|
| 61 |
+
# IMPROVED ANSWER EXTRACTION
|
| 62 |
# ===============================
|
| 63 |
|
| 64 |
def extract_answer(text):
|
| 65 |
+
"""
|
| 66 |
+
Improved answer extraction for GAIA Level 1:
|
| 67 |
+
- Return first number if present
|
| 68 |
+
- Otherwise, return first meaningful short phrase
|
| 69 |
+
- Strip extra spaces, punctuation
|
| 70 |
+
"""
|
| 71 |
import re
|
| 72 |
+
import string
|
| 73 |
+
|
| 74 |
+
if not text:
|
| 75 |
+
return ""
|
| 76 |
+
|
| 77 |
+
# 1️⃣ Try numbers first
|
| 78 |
numbers = re.findall(r"\b\d+(?:\.\d+)?\b", text)
|
| 79 |
if numbers:
|
| 80 |
return numbers[0]
|
| 81 |
+
|
| 82 |
+
# 2️⃣ Clean text for short phrase
|
| 83 |
+
text = text.strip()
|
| 84 |
+
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 85 |
words = text.split()
|
| 86 |
+
|
| 87 |
+
# Return first 1–3 words for short answers
|
| 88 |
+
return " ".join(words[:3]).strip()
|
| 89 |
|
| 90 |
# ===============================
|
| 91 |
# FETCH QUESTIONS
|
|
|
|
| 108 |
}
|
| 109 |
response = requests.post(f"{BASE_URL}/submit", json=payload)
|
| 110 |
print("Server response:", response.text)
|
| 111 |
+
return response.text # Return server response to output box
|
| 112 |
|
| 113 |
# ===============================
|
| 114 |
# MAIN PIPELINE
|
|
|
|
| 129 |
"submitted_answer": result.strip()
|
| 130 |
})
|
| 131 |
server_response = submit_answers(answers)
|
| 132 |
+
return server_response
|
| 133 |
|
| 134 |
# ===============================
|
| 135 |
# GRADIO BLOCKS UI
|