dmfelder commited on
Commit
eb3c193
·
verified ·
1 Parent(s): f9065e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -23
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 the model (will take ~30 seconds on first run)
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
- # Helper to identify questions we should skip
17
- def is_unanswerable_question(question: str) -> bool:
18
- image_clues = ["image", "diagram", "shown", "displayed", "provided", "refer to"]
19
- bad_phrases = ["review the chess", "what does the image", "based on this chart"]
20
- return any(p in question.lower() for p in image_clues + bad_phrases)
 
 
 
21
 
22
- # Answer generator with filtering and fallback
23
  def generate_answer(task):
24
- question = task["question"]
25
- if is_unanswerable_question(question):
26
  return "SKIPPED"
27
-
28
- prompt = f"Answer with only the correct value (one word): {question}"
29
  result = qa_pipeline(prompt)[0]["generated_text"]
30
- answer_line = result.strip().split("\n")[0].strip()
31
- word = re.findall(r"\b[A-Za-z\-']+\b", answer_line)
32
- return word[-1].capitalize() if word else "Unknown"
 
 
33
 
34
- # Display-only test
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
- # Submit to the scoring API
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
- # Gradio UI
59
  with gr.Blocks() as demo:
60
- gr.Markdown("# 🤖 Unit 4 Agent Test UI — Clean Version with Filter")
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 Random Question")
66
- submit_output = gr.Textbox(lines=10, label="Scoring Response")
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)