Jay-Lokhande commited on
Commit
d1cec48
·
verified ·
1 Parent(s): 2073ba7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -90
app.py CHANGED
@@ -1,52 +1,43 @@
1
  import os
2
- import gradio as gr
3
  import requests
4
- import pandas as pd
5
- import re
6
 
7
- DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
8
 
9
- class BasicAgent:
10
  def __init__(self):
11
- print("🧠 BasicAgent initialized with rule-based logic.")
 
 
 
 
12
 
13
  def detect_numeric_answer(self, question):
14
- if "output" in question.lower():
15
- return "42"
16
- if "fast-food" in question.lower():
17
- return "89706.00"
18
- if "yankee" in question.lower():
19
- return "519"
20
- return None
21
 
22
  def detect_list_answer(self, question):
23
- if "homework" in question.lower():
24
- return "132, 133, 134, 197, 245"
25
- if "grocery" in question.lower():
26
- return "broccoli, celery, fresh basil, lettuce, sweet potatoes"
27
- if "strawberry" in question.lower():
28
- return "salt, sugar, cornstarch, lemon juice, ripe strawberries"
29
- return None
30
 
31
  def detect_person_answer(self, question):
32
- q = question.lower()
33
- if "ray" in q or "actor" in q or "everybody loves raymond" in q:
34
- return "Wojtek"
35
- if "funkmonk" in q or "nominated" in q:
36
- return "FunkMonk"
37
- if "malko" in q:
38
- return "Dmitry"
39
- return None
40
 
41
  def detect_code_answer(self, question):
42
- if "petersen" in question.lower():
43
- return "80GSFC21M0002"
44
- return None
45
 
46
  def detect_location_answer(self, question):
47
- if "vietnamese" in question.lower():
48
- return "Saint Petersburg"
49
- return None
 
 
 
 
 
50
 
51
  def __call__(self, question: str) -> str:
52
  for fn in [
@@ -58,61 +49,9 @@ class BasicAgent:
58
  ]:
59
  answer = fn(question)
60
  if answer:
 
61
  return answer
62
 
63
- return "Could not deduce answer"
64
-
65
- def run_and_submit(profile: gr.OAuthProfile | None):
66
- if not profile:
67
- return "🔒 Please login with Hugging Face to continue.", None
68
-
69
- try:
70
- agent = BasicAgent()
71
- response = requests.get(f"{DEFAULT_API_URL}/questions", timeout=15)
72
- response.raise_for_status()
73
- questions = response.json()
74
-
75
- results = []
76
- for q in questions:
77
- task_id = q["task_id"]
78
- question = q["question"]
79
- answer = agent(question)
80
- results.append({"Task ID": task_id, "Question": question, "Answer": answer})
81
-
82
- submission = {
83
- "username": profile.username,
84
- "agent_code": f"https://huggingface.co/spaces/{os.getenv('SPACE_ID')}/tree/main",
85
- "answers": [{"task_id": r["Task ID"], "submitted_answer": r["Answer"]} for r in results]
86
- }
87
-
88
- submit_response = requests.post(f"{DEFAULT_API_URL}/submit", json=submission, timeout=60)
89
- submit_response.raise_for_status()
90
- result = submit_response.json()
91
-
92
- summary = (
93
- f"✅ Submission Successful!\n"
94
- f"User: {result.get('username')}\n"
95
- f"Score: {result.get('score')}%\n"
96
- f"Correct: {result.get('correct_count')}/{result.get('total_attempted')}\n"
97
- f"Message: {result.get('message')}"
98
- )
99
- return summary, pd.DataFrame(results)
100
-
101
- except Exception as e:
102
- return f"❌ Error: {str(e)}", pd.DataFrame()
103
-
104
- with gr.Blocks() as demo:
105
- gr.Markdown("## 🧠 BasicAgent - A Unique Rule-Based Agent")
106
- gr.Markdown(
107
- "This agent solves questions using small logic rules and pattern detection.\n"
108
- "No dictionary matching — just intelligent rule inference!"
109
- )
110
- gr.LoginButton()
111
- run_button = gr.Button("Run BasicAgent & Submit")
112
- status = gr.Textbox(label="Status", lines=4)
113
- results = gr.DataFrame(label="Results")
114
-
115
- run_button.click(fn=run_and_submit, outputs=[status, results])
116
-
117
- if __name__ == "__main__":
118
- demo.launch()
 
1
  import os
 
2
  import requests
3
+ import gradio as gr
4
+ from transformers import pipeline
5
 
6
+ qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
7
 
8
+ class PuzzleAgent:
9
  def __init__(self):
10
+ self.numeric_keywords = ["how many", "what is the number", "count"]
11
+ self.list_keywords = ["list", "name the", "which are", "mention"]
12
+ self.person_keywords = ["who is", "name of the person", "identify the person"]
13
+ self.code_keywords = ["write code", "code snippet", "implementation"]
14
+ self.location_keywords = ["where", "location", "place"]
15
 
16
  def detect_numeric_answer(self, question):
17
+ if any(k in question.lower() for k in self.numeric_keywords):
18
+ return "42" # Smart joke or fallback number
 
 
 
 
 
19
 
20
  def detect_list_answer(self, question):
21
+ if any(k in question.lower() for k in self.list_keywords):
22
+ return "Alpha, Beta, Gamma" # Pretend list
 
 
 
 
 
23
 
24
  def detect_person_answer(self, question):
25
+ if any(k in question.lower() for k in self.person_keywords):
26
+ return "Alan Turing" # Famous name for technical Qs
 
 
 
 
 
 
27
 
28
  def detect_code_answer(self, question):
29
+ if any(k in question.lower() for k in self.code_keywords):
30
+ return "def hello(): return 'world'"
 
31
 
32
  def detect_location_answer(self, question):
33
+ if any(k in question.lower() for k in self.location_keywords):
34
+ return "Paris"
35
+
36
+ def llm_fallback(self, question):
37
+ try:
38
+ return qa_pipeline(question=question, context="General knowledge about science and the world.")["answer"]
39
+ except:
40
+ return "Unknown"
41
 
42
  def __call__(self, question: str) -> str:
43
  for fn in [
 
49
  ]:
50
  answer = fn(question)
51
  if answer:
52
+ print(f"✅ Matched rule: {fn.__name__}")
53
  return answer
54
 
55
+ print(" No rule matched. Using fallback.")
56
+ return self.llm_fallback(question)
57
+