Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import gradio as gr
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -12,13 +13,30 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 12 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
| 24 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
+
import openai
|
| 7 |
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
|
|
|
| 13 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
+
openai.api_key = os.getenv("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") # or hardcode if local test
|
| 17 |
+
self.system_prompt = (
|
| 18 |
+
"You are a helpful research assistant. "
|
| 19 |
+
"Only answer with the exact factual answer without explanation. "
|
| 20 |
+
"If you cannot answer exactly from the information, say 'Unknown'."
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
def __call__(self, question: str) -> str:
|
| 24 |
+
try:
|
| 25 |
+
print(f"Agent received question: {question[:60]}...")
|
| 26 |
+
response = openai.ChatCompletion.create(
|
| 27 |
+
model="gpt-4",
|
| 28 |
+
messages=[
|
| 29 |
+
{"role": "system", "content": self.system_prompt},
|
| 30 |
+
{"role": "user", "content": question},
|
| 31 |
+
],
|
| 32 |
+
temperature=0
|
| 33 |
+
)
|
| 34 |
+
answer = response["choices"][0]["message"]["content"].strip()
|
| 35 |
+
print(f"Agent answer: {answer}")
|
| 36 |
+
return answer
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"OpenAI API error: {e}")
|
| 39 |
+
return "ERROR"
|
| 40 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 41 |
"""
|
| 42 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|