File size: 3,470 Bytes
10e9b7d
eccf8e4
904e0bd
 
 
 
 
 
 
 
 
3c4371f
904e0bd
 
 
 
 
7e4a06b
904e0bd
 
 
 
 
 
 
 
 
 
 
 
3c4371f
904e0bd
 
 
 
e80aab9
904e0bd
 
e80aab9
904e0bd
 
e80aab9
904e0bd
 
 
 
 
 
 
 
 
 
 
 
e80aab9
904e0bd
e80aab9
904e0bd
 
 
 
 
 
e80aab9
904e0bd
 
 
e80aab9
904e0bd
 
 
 
 
 
 
e80aab9
904e0bd
 
e80aab9
904e0bd
7d65c66
904e0bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d65c66
904e0bd
7d65c66
904e0bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c4371f
904e0bd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import os
import requests
import gradio as gr
from dotenv import load_dotenv

# Load API key dari file .env
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

# GAIA API Endpoint
GAIA_API = "https://agents-course-unit4-scoring.hf.space"

# Ambil semua pertanyaan
def get_questions():
    response = requests.get(f"{GAIA_API}/questions")
    if response.status_code == 200:
        return response.json()
    else:
        raise Exception("Gagal mengambil pertanyaan dari GAIA API")

# Ambil file multimodal (jika ada)
def get_file(task_id):
    response = requests.get(f"{GAIA_API}/files/{task_id}")
    if response.status_code == 200:
        file_path = f"temp_{task_id}"
        with open(file_path, "wb") as f:
            f.write(response.content)
        return file_path
    else:
        return None

# Gunakan OpenAI atau model Hugging Face untuk menjawab
def generate_answer(question_text, context=None):
    prompt = f"""
Jawablah pertanyaan berikut dengan jelas dan akurat:

Pertanyaan:
{question_text}

Tambahan konteks:
{context if context else "Tidak ada"}

Jawaban:
    """
    # Jika pakai OpenAI
    import openai
    openai.api_key = OPENAI_API_KEY
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[
            {"role": "user", "content": prompt}
        ],
        temperature=0.7,
        max_tokens=300
    )
    return response.choices[0].message.content.strip()

# Fungsi utama: ambil pertanyaan dan jawab
def answer_question(index):
    try:
        questions = get_questions()
        if index < 0 or index >= len(questions):
            return "Indeks pertanyaan tidak valid", "", ""

        question = questions[index]
        task_id = question["task_id"]
        q_text = question["question"]

        # Cek dan ambil file multimodal jika ada
        file_path = None
        if question.get("has_file"):
            file_path = get_file(task_id)
        
        # Bisa dikembangkan: gunakan image/audio processing untuk context
        context = f"File tersedia: {file_path}" if file_path else ""

        # Generate jawaban
        answer = generate_answer(q_text, context)

        return q_text, context, answer

    except Exception as e:
        return f"Terjadi kesalahan: {str(e)}", "", ""

# Untuk mengirim jawaban ke leaderboard (opsional)
def submit_to_leaderboard(username, agent_code_url, task_id, answer):
    data = {
        "username": username,
        "agent_code": agent_code_url,
        "answers": [
            {
                "task_id": task_id,
                "submitted_answer": answer
            }
        ]
    }
    response = requests.post(f"{GAIA_API}/submit", json=data)
    if response.status_code == 200:
        return f"Berhasil submit! Hasil: {response.json()}"
    else:
        return f"Gagal submit: {response.text}"

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("# 🤖 GAIA Question Answering Agent")
    gr.Markdown("Masukkan indeks pertanyaan (0-19) untuk dijawab oleh agen AI.")
    
    index_input = gr.Number(label="Index Pertanyaan (0-19)", value=0, precision=0)
    get_answer_btn = gr.Button("Dapatkan Jawaban")

    q_output = gr.Textbox(label="Pertanyaan")
    file_info = gr.Textbox(label="Info File (jika ada)")
    answer_output = gr.Textbox(label="Jawaban Agen")

    get_answer_btn.click(
        fn=answer_question,
        inputs=[index_input],
        outputs=[q_output, file_info, answer_output]
    )

demo.launch()