Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,39 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
-
|
| 6 |
|
| 7 |
# --- Constants ---
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
# --- Basic Agent Definition ---
|
| 11 |
-
# Modified to use
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
def __call__(self, question: str) -> str:
|
| 19 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 27 |
"""
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import pandas as pd
|
| 5 |
+
import openai # NEW: import OpenAI SDK
|
| 6 |
|
| 7 |
# --- Constants ---
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
# --- Basic Agent Definition ---
|
| 11 |
+
# Modified to use OpenAI GPT-4 API for Q&A
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
| 14 |
+
self.api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
+
if not self.api_key:
|
| 16 |
+
raise ValueError("OPENAI_API_KEY environment variable is not set!")
|
| 17 |
+
openai.api_key = self.api_key
|
| 18 |
+
print("BasicAgent initialized with OpenAI API key.")
|
| 19 |
|
| 20 |
def __call__(self, question: str) -> str:
|
| 21 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 22 |
+
try:
|
| 23 |
+
response = openai.ChatCompletion.create(
|
| 24 |
+
model="gpt-4",
|
| 25 |
+
messages=[
|
| 26 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 27 |
+
{"role": "user", "content": question}
|
| 28 |
+
],
|
| 29 |
+
max_tokens=200,
|
| 30 |
+
temperature=0
|
| 31 |
+
)
|
| 32 |
+
answer = response['choices'][0]['message']['content'].strip()
|
| 33 |
+
print(f"Agent returning answer: {answer}")
|
| 34 |
+
return answer
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Error calling OpenAI API: {e}")
|
| 37 |
+
return "Sorry, I couldn't answer that."
|
| 38 |
|
| 39 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 40 |
"""
|