Spaces:
Sleeping
Sleeping
test2
Browse files
app.py
CHANGED
@@ -1,121 +1,29 @@
|
|
|
|
1 |
import os
|
2 |
-
import requests
|
3 |
-
import gradio as gr
|
4 |
-
from dotenv import load_dotenv
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
def
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
f
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
# Gunakan OpenAI atau model Hugging Face untuk menjawab
|
33 |
-
def generate_answer(question_text, context=None):
|
34 |
-
prompt = f"""
|
35 |
-
Jawablah pertanyaan berikut dengan jelas dan akurat:
|
36 |
-
|
37 |
-
Pertanyaan:
|
38 |
-
{question_text}
|
39 |
-
|
40 |
-
Tambahan konteks:
|
41 |
-
{context if context else "Tidak ada"}
|
42 |
-
|
43 |
-
Jawaban:
|
44 |
-
"""
|
45 |
-
# Jika pakai OpenAI
|
46 |
-
import openai
|
47 |
-
openai.api_key = OPENAI_API_KEY
|
48 |
-
response = openai.ChatCompletion.create(
|
49 |
-
model="gpt-4",
|
50 |
-
messages=[
|
51 |
-
{"role": "user", "content": prompt}
|
52 |
-
],
|
53 |
-
temperature=0.7,
|
54 |
-
max_tokens=300
|
55 |
-
)
|
56 |
-
return response.choices[0].message.content.strip()
|
57 |
-
|
58 |
-
# Fungsi utama: ambil pertanyaan dan jawab
|
59 |
-
def answer_question(index):
|
60 |
-
try:
|
61 |
-
questions = get_questions()
|
62 |
-
if index < 0 or index >= len(questions):
|
63 |
-
return "Indeks pertanyaan tidak valid", "", ""
|
64 |
-
|
65 |
-
question = questions[index]
|
66 |
-
task_id = question["task_id"]
|
67 |
-
q_text = question["question"]
|
68 |
-
|
69 |
-
# Cek dan ambil file multimodal jika ada
|
70 |
-
file_path = None
|
71 |
-
if question.get("has_file"):
|
72 |
-
file_path = get_file(task_id)
|
73 |
-
|
74 |
-
# Bisa dikembangkan: gunakan image/audio processing untuk context
|
75 |
-
context = f"File tersedia: {file_path}" if file_path else ""
|
76 |
-
|
77 |
-
# Generate jawaban
|
78 |
-
answer = generate_answer(q_text, context)
|
79 |
-
|
80 |
-
return q_text, context, answer
|
81 |
-
|
82 |
-
except Exception as e:
|
83 |
-
return f"Terjadi kesalahan: {str(e)}", "", ""
|
84 |
-
|
85 |
-
# Untuk mengirim jawaban ke leaderboard (opsional)
|
86 |
-
def submit_to_leaderboard(username, agent_code_url, task_id, answer):
|
87 |
-
data = {
|
88 |
-
"username": username,
|
89 |
-
"agent_code": agent_code_url,
|
90 |
-
"answers": [
|
91 |
-
{
|
92 |
-
"task_id": task_id,
|
93 |
-
"submitted_answer": answer
|
94 |
-
}
|
95 |
-
]
|
96 |
-
}
|
97 |
-
response = requests.post(f"{GAIA_API}/submit", json=data)
|
98 |
-
if response.status_code == 200:
|
99 |
-
return f"Berhasil submit! Hasil: {response.json()}"
|
100 |
-
else:
|
101 |
-
return f"Gagal submit: {response.text}"
|
102 |
-
|
103 |
-
# Gradio UI
|
104 |
-
with gr.Blocks() as demo:
|
105 |
-
gr.Markdown("# 🤖 GAIA Question Answering Agent")
|
106 |
-
gr.Markdown("Masukkan indeks pertanyaan (0-19) untuk dijawab oleh agen AI.")
|
107 |
-
|
108 |
-
index_input = gr.Number(label="Index Pertanyaan (0-19)", value=0, precision=0)
|
109 |
-
get_answer_btn = gr.Button("Dapatkan Jawaban")
|
110 |
-
|
111 |
-
q_output = gr.Textbox(label="Pertanyaan")
|
112 |
-
file_info = gr.Textbox(label="Info File (jika ada)")
|
113 |
-
answer_output = gr.Textbox(label="Jawaban Agen")
|
114 |
-
|
115 |
-
get_answer_btn.click(
|
116 |
-
fn=answer_question,
|
117 |
-
inputs=[index_input],
|
118 |
-
outputs=[q_output, file_info, answer_output]
|
119 |
-
)
|
120 |
-
|
121 |
-
demo.launch()
|
|
|
1 |
+
from openai import OpenAI
|
2 |
import os
|
|
|
|
|
|
|
3 |
|
4 |
+
class BasicAgent:
|
5 |
+
def __init__(self):
|
6 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
7 |
+
if not api_key:
|
8 |
+
raise ValueError("OPENAI_API_KEY environment variable not set.")
|
9 |
+
self.client = OpenAI(api_key=api_key)
|
10 |
+
print("BasicAgent (GPT-based) initialized.")
|
11 |
+
|
12 |
+
def __call__(self, question: str) -> str:
|
13 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
14 |
+
try:
|
15 |
+
response = self.client.chat.completions.create(
|
16 |
+
model="gpt-4", # Bisa diganti dengan "gpt-3.5-turbo" jika quota terbatas
|
17 |
+
messages=[
|
18 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
19 |
+
{"role": "user", "content": question}
|
20 |
+
],
|
21 |
+
temperature=0.7,
|
22 |
+
max_tokens=500,
|
23 |
+
)
|
24 |
+
answer = response.choices[0].message.content.strip()
|
25 |
+
print(f"Agent returning answer: {answer[:100]}...")
|
26 |
+
return answer
|
27 |
+
except Exception as e:
|
28 |
+
print(f"Error during OpenAI completion: {e}")
|
29 |
+
return f"[ERROR from agent: {str(e)}]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|