Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
from transformers import pipeline
|
| 4 |
import re
|
|
|
|
| 5 |
|
| 6 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 7 |
|
| 8 |
-
# Load
|
| 9 |
qa_pipeline = pipeline(
|
| 10 |
"text-generation",
|
| 11 |
model="tiiuae/falcon-7b-instruct",
|
|
@@ -13,35 +13,38 @@ qa_pipeline = pipeline(
|
|
| 13 |
do_sample=False,
|
| 14 |
)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
def generate_answer(task):
|
| 24 |
-
|
| 25 |
-
if is_unanswerable_question(question):
|
| 26 |
return "SKIPPED"
|
| 27 |
-
|
| 28 |
-
prompt = f"Answer
|
| 29 |
result = qa_pipeline(prompt)[0]["generated_text"]
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
def test_random_question():
|
| 36 |
task = requests.get(f"{API_URL}/random-question").json()
|
| 37 |
answer = generate_answer(task)
|
| 38 |
return f"Task ID: {task['task_id']}\n\nQuestion: {task['question']}\n\nYour Answer: {answer}"
|
| 39 |
|
| 40 |
-
#
|
| 41 |
def submit_example():
|
| 42 |
task = requests.get(f"{API_URL}/random-question").json()
|
| 43 |
answer = generate_answer(task)
|
| 44 |
-
|
| 45 |
payload = {
|
| 46 |
"username": "dmfelder",
|
| 47 |
"agent_code": "https://huggingface.co/spaces/dmfelder/unit4-agent",
|
|
@@ -55,15 +58,15 @@ def submit_example():
|
|
| 55 |
r = requests.post(f"{API_URL}/submit", json=payload)
|
| 56 |
return f"Submitted!\nResponse: {r.json()}"
|
| 57 |
|
| 58 |
-
#
|
| 59 |
with gr.Blocks() as demo:
|
| 60 |
-
gr.Markdown("#
|
| 61 |
|
| 62 |
-
test_btn = gr.Button("🧪 Test Random Question")
|
| 63 |
test_output = gr.Textbox(lines=10, label="Task + Answer")
|
| 64 |
|
| 65 |
-
submit_btn = gr.Button("🚀 Submit
|
| 66 |
-
submit_output = gr.Textbox(lines=10, label="
|
| 67 |
|
| 68 |
test_btn.click(fn=test_random_question, outputs=test_output)
|
| 69 |
submit_btn.click(fn=submit_example, outputs=submit_output)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
import re
|
| 4 |
+
from transformers import pipeline
|
| 5 |
|
| 6 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 7 |
|
| 8 |
+
# Load Mistral model
|
| 9 |
qa_pipeline = pipeline(
|
| 10 |
"text-generation",
|
| 11 |
model="tiiuae/falcon-7b-instruct",
|
|
|
|
| 13 |
do_sample=False,
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Filters out problematic tasks (image/audio/code/math)
|
| 17 |
+
def is_safe_question(text):
|
| 18 |
+
text = text.lower()
|
| 19 |
+
return not any(keyword in text for keyword in [
|
| 20 |
+
"image", "audio", "attached", "algebraic", "recording",
|
| 21 |
+
"code", "function", "calculate", "math", "python",
|
| 22 |
+
"diagram", "equation", "plot", "solve", "mp3", "jpg", "png"
|
| 23 |
+
])
|
| 24 |
|
| 25 |
+
# Extracts clean short answer
|
| 26 |
def generate_answer(task):
|
| 27 |
+
if not is_safe_question(task["question"]):
|
|
|
|
| 28 |
return "SKIPPED"
|
| 29 |
+
|
| 30 |
+
prompt = f"Answer the question briefly with a single name, word, or number only: {task['question']}"
|
| 31 |
result = qa_pipeline(prompt)[0]["generated_text"]
|
| 32 |
+
|
| 33 |
+
# Clean final token
|
| 34 |
+
answer = result.strip().split("\n")[0].strip()
|
| 35 |
+
final = re.findall(r"\b[A-Za-z0-9\-']+\b", answer)
|
| 36 |
+
return final[-1].capitalize() if final else "SKIPPED"
|
| 37 |
|
| 38 |
+
# Manual test button
|
| 39 |
def test_random_question():
|
| 40 |
task = requests.get(f"{API_URL}/random-question").json()
|
| 41 |
answer = generate_answer(task)
|
| 42 |
return f"Task ID: {task['task_id']}\n\nQuestion: {task['question']}\n\nYour Answer: {answer}"
|
| 43 |
|
| 44 |
+
# Submission to leaderboard
|
| 45 |
def submit_example():
|
| 46 |
task = requests.get(f"{API_URL}/random-question").json()
|
| 47 |
answer = generate_answer(task)
|
|
|
|
| 48 |
payload = {
|
| 49 |
"username": "dmfelder",
|
| 50 |
"agent_code": "https://huggingface.co/spaces/dmfelder/unit4-agent",
|
|
|
|
| 58 |
r = requests.post(f"{API_URL}/submit", json=payload)
|
| 59 |
return f"Submitted!\nResponse: {r.json()}"
|
| 60 |
|
| 61 |
+
# UI
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
+
gr.Markdown("# 🧠 Unit 4 Agent — Clean & Simple Mode")
|
| 64 |
|
| 65 |
+
test_btn = gr.Button("🧪 Test a Random Question")
|
| 66 |
test_output = gr.Textbox(lines=10, label="Task + Answer")
|
| 67 |
|
| 68 |
+
submit_btn = gr.Button("🚀 Submit Answer")
|
| 69 |
+
submit_output = gr.Textbox(lines=10, label="Submission Response")
|
| 70 |
|
| 71 |
test_btn.click(fn=test_random_question, outputs=test_output)
|
| 72 |
submit_btn.click(fn=submit_example, outputs=submit_output)
|