Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,32 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import openai
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
|
| 7 |
# Constants
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
-
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Assuming you're using OpenAI's GPT model for the agent.
|
| 10 |
|
| 11 |
-
# Basic Agent Definition
|
| 12 |
class BasicAgent:
|
| 13 |
def __init__(self):
|
| 14 |
print("BasicAgent initialized.")
|
| 15 |
-
|
| 16 |
|
| 17 |
def __call__(self, question: str) -> str:
|
| 18 |
print(f"Agent received question: {question[:50]}...")
|
| 19 |
-
|
| 20 |
-
# Use OpenAI GPT to generate a response for the question
|
| 21 |
try:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
stop=None,
|
| 28 |
-
temperature=0.7,
|
| 29 |
-
)
|
| 30 |
-
fixed_answer = response.choices[0].text.strip()
|
| 31 |
-
print(f"Agent returning answer: {fixed_answer}")
|
| 32 |
-
return fixed_answer
|
| 33 |
except Exception as e:
|
| 34 |
-
print(f"Error while
|
| 35 |
return f"Error: {e}"
|
| 36 |
|
| 37 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 38 |
-
""
|
| 39 |
-
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 40 |
-
and displays the results.
|
| 41 |
-
"""
|
| 42 |
-
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 43 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 44 |
|
| 45 |
if profile:
|
| 46 |
username = f"{profile.username}"
|
|
@@ -103,7 +89,11 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 103 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 104 |
|
| 105 |
# 4. Prepare Submission
|
| 106 |
-
submission_data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 108 |
print(status_update)
|
| 109 |
|
|
@@ -134,7 +124,6 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 134 |
results_df = pd.DataFrame(results_log)
|
| 135 |
return status_message, results_df
|
| 136 |
|
| 137 |
-
|
| 138 |
# Gradio Interface
|
| 139 |
with gr.Blocks() as demo:
|
| 140 |
gr.Markdown("# Basic Agent Evaluation Runner")
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
# Constants
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 9 |
|
| 10 |
+
# Basic Agent Definition using HF Inference API
|
| 11 |
class BasicAgent:
|
| 12 |
def __init__(self):
|
| 13 |
print("BasicAgent initialized.")
|
| 14 |
+
self.client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
|
| 15 |
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question: {question[:50]}...")
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
+
prompt = f"[INST] {question.strip()} [/INST]"
|
| 20 |
+
response = self.client.text_generation(prompt=prompt, max_new_tokens=300, temperature=0.7)
|
| 21 |
+
answer = response.strip()
|
| 22 |
+
print(f"Agent returning answer: {answer}")
|
| 23 |
+
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
+
print(f"Error while querying HF model: {e}")
|
| 26 |
return f"Error: {e}"
|
| 27 |
|
| 28 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 29 |
+
space_id = os.getenv("SPACE_ID")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
if profile:
|
| 32 |
username = f"{profile.username}"
|
|
|
|
| 89 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 90 |
|
| 91 |
# 4. Prepare Submission
|
| 92 |
+
submission_data = {
|
| 93 |
+
"username": username.strip(),
|
| 94 |
+
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
| 95 |
+
"answers": answers_payload
|
| 96 |
+
}
|
| 97 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 98 |
print(status_update)
|
| 99 |
|
|
|
|
| 124 |
results_df = pd.DataFrame(results_log)
|
| 125 |
return status_message, results_df
|
| 126 |
|
|
|
|
| 127 |
# Gradio Interface
|
| 128 |
with gr.Blocks() as demo:
|
| 129 |
gr.Markdown("# Basic Agent Evaluation Runner")
|