Spaces:
Sleeping
Sleeping
test4
Browse files
app.py
CHANGED
|
@@ -3,73 +3,66 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import openai
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 8 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
-
|
| 10 |
-
# === Agent GPT-4o ===
|
| 11 |
class GPTAgent:
|
| 12 |
def __init__(self):
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
def __call__(self, question: str) -> str:
|
| 16 |
try:
|
| 17 |
response = openai.chat.completions.create(
|
| 18 |
-
model=
|
| 19 |
messages=[
|
| 20 |
-
{"role": "system", "content": "You are
|
| 21 |
-
{"role": "user", "content": question}
|
| 22 |
-
]
|
| 23 |
-
temperature=0.2,
|
| 24 |
-
max_tokens=500,
|
| 25 |
)
|
| 26 |
return response.choices[0].message.content.strip()
|
| 27 |
except Exception as e:
|
| 28 |
return f"ERROR: {e}"
|
| 29 |
|
| 30 |
-
#
|
| 31 |
def run_and_submit_all(user_profile):
|
| 32 |
-
username = user_profile
|
| 33 |
-
questions_url = f"{DEFAULT_API_URL}/questions"
|
| 34 |
-
submit_url = f"{DEFAULT_API_URL}/submit"
|
| 35 |
-
|
| 36 |
agent = GPTAgent()
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
response.raise_for_status()
|
| 41 |
-
questions = response.json()
|
| 42 |
except Exception as e:
|
| 43 |
-
return f"Error fetching questions: {e}", []
|
| 44 |
|
|
|
|
| 45 |
results = []
|
| 46 |
-
for item in questions:
|
| 47 |
-
task_id = item.get("task_id", "")
|
| 48 |
-
question = item.get("question", "")
|
| 49 |
-
answer = agent(question)
|
| 50 |
-
results.append([task_id, question, answer])
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
payload = {
|
| 54 |
"user": username,
|
| 55 |
-
"answers":
|
| 56 |
}
|
| 57 |
|
| 58 |
try:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
)
|
| 67 |
except Exception as e:
|
| 68 |
-
return f"Error submitting answers: {e}
|
| 69 |
-
|
| 70 |
-
return status, results
|
| 71 |
|
| 72 |
-
#
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
gr.Markdown("# GAIA Benchmark Agent with GPT-4o")
|
| 75 |
gr.Markdown(
|
|
@@ -80,12 +73,12 @@ with gr.Blocks() as demo:
|
|
| 80 |
"""
|
| 81 |
)
|
| 82 |
|
| 83 |
-
|
| 84 |
run_btn = gr.Button("Run Evaluation & Submit")
|
| 85 |
status = gr.Textbox(label="Status", interactive=False, lines=4)
|
| 86 |
-
results = gr.DataFrame(headers=["Task ID", "Question", "Answer"]
|
| 87 |
|
| 88 |
-
run_btn.click(fn=run_and_submit_all, inputs=[
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
demo.launch()
|
|
|
|
| 3 |
import requests
|
| 4 |
import openai
|
| 5 |
|
| 6 |
+
# Gunakan OpenAI GPT untuk menjawab pertanyaan
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
class GPTAgent:
|
| 8 |
def __init__(self):
|
| 9 |
+
self.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
openai.api_key = self.api_key
|
| 11 |
+
self.model = "gpt-4o" # atau "gpt-3.5-turbo" jika tidak punya akses
|
| 12 |
|
| 13 |
def __call__(self, question: str) -> str:
|
| 14 |
try:
|
| 15 |
response = openai.chat.completions.create(
|
| 16 |
+
model=self.model,
|
| 17 |
messages=[
|
| 18 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 19 |
+
{"role": "user", "content": question},
|
| 20 |
+
]
|
|
|
|
|
|
|
| 21 |
)
|
| 22 |
return response.choices[0].message.content.strip()
|
| 23 |
except Exception as e:
|
| 24 |
return f"ERROR: {e}"
|
| 25 |
|
| 26 |
+
# Fungsi utama untuk evaluasi & submit
|
| 27 |
def run_and_submit_all(user_profile):
|
| 28 |
+
username = user_profile if user_profile else "anonymous"
|
|
|
|
|
|
|
|
|
|
| 29 |
agent = GPTAgent()
|
| 30 |
|
| 31 |
+
questions_url = "https://agents-course-unit4-scoring.hf.space/questions"
|
| 32 |
+
submit_url = "https://agents-course-unit4-scoring.hf.space/submit"
|
| 33 |
+
|
| 34 |
try:
|
| 35 |
+
questions = requests.get(questions_url, timeout=10).json()
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"❌ Error fetching questions: {e}", []
|
| 38 |
|
| 39 |
+
answers = []
|
| 40 |
results = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
for q in questions:
|
| 43 |
+
task_id = q["task_id"]
|
| 44 |
+
question_text = q["question"]
|
| 45 |
+
answer = agent(question_text)
|
| 46 |
+
answers.append({"task_id": task_id, "answer": answer})
|
| 47 |
+
results.append([task_id, question_text, answer])
|
| 48 |
+
|
| 49 |
payload = {
|
| 50 |
"user": username,
|
| 51 |
+
"answers": answers
|
| 52 |
}
|
| 53 |
|
| 54 |
try:
|
| 55 |
+
res = requests.post(submit_url, json=payload, timeout=10)
|
| 56 |
+
res.raise_for_status()
|
| 57 |
+
score = res.json().get("score", 0.0)
|
| 58 |
+
correct = res.json().get("correct", 0)
|
| 59 |
+
total = res.json().get("total", len(questions))
|
| 60 |
+
status = f"✅ User: {username}\nScore: {score:.1f}%\nCorrect: {correct}/{total}"
|
| 61 |
+
return status, results
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
+
return f"❌ Error submitting answers: {e}", results
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
# UI Gradio
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
gr.Markdown("# GAIA Benchmark Agent with GPT-4o")
|
| 68 |
gr.Markdown(
|
|
|
|
| 73 |
"""
|
| 74 |
)
|
| 75 |
|
| 76 |
+
login = gr.LoginButton()
|
| 77 |
run_btn = gr.Button("Run Evaluation & Submit")
|
| 78 |
status = gr.Textbox(label="Status", interactive=False, lines=4)
|
| 79 |
+
results = gr.DataFrame(headers=["Task ID", "Question", "Answer"])
|
| 80 |
|
| 81 |
+
run_btn.click(fn=run_and_submit_all, inputs=[login], outputs=[status, results])
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
demo.launch()
|