Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
-
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
# --- Basic Agent Definition ---
|
| 13 |
-
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 14 |
-
from transformers import pipeline
|
| 15 |
-
|
| 16 |
class BasicAgent:
|
| 17 |
def __init__(self):
|
| 18 |
print("Loading FLAN-T5 base model...")
|
| 19 |
self.pipeline = pipeline(
|
| 20 |
"text2text-generation",
|
| 21 |
-
model
|
| 22 |
max_new_tokens=128,
|
| 23 |
temperature=0.3
|
| 24 |
)
|
|
@@ -34,7 +29,8 @@ class BasicAgent:
|
|
| 34 |
return self.reverse_sentence(question)
|
| 35 |
else:
|
| 36 |
return self.model_response(question)
|
| 37 |
-
|
|
|
|
| 38 |
prompt = f"Please answer step by step:\n{question}"
|
| 39 |
output = self.pipeline(prompt)[0]["generated_text"]
|
| 40 |
return output.strip().split("Answer:")[-1].strip()
|
|
@@ -48,12 +44,10 @@ class BasicAgent:
|
|
| 48 |
return f"Could not find info: {e}"
|
| 49 |
|
| 50 |
def parse_excel(self, task_id: str) -> str:
|
| 51 |
-
import requests
|
| 52 |
-
import pandas as pd
|
| 53 |
try:
|
| 54 |
file_url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 55 |
df = pd.read_excel(file_url)
|
| 56 |
-
#
|
| 57 |
food_sales = df[df['category'] == 'food']['sales'].sum()
|
| 58 |
return f"${food_sales:.2f}"
|
| 59 |
except Exception as e:
|
|
@@ -65,32 +59,31 @@ class BasicAgent:
|
|
| 65 |
return reversed_text
|
| 66 |
|
| 67 |
|
| 68 |
-
def run_and_submit_all(
|
| 69 |
"""
|
| 70 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 71 |
and displays the results.
|
| 72 |
"""
|
| 73 |
-
#
|
| 74 |
-
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 75 |
|
| 76 |
if profile:
|
| 77 |
-
username= f"{profile.username}"
|
| 78 |
-
print(f"User
|
| 79 |
else:
|
| 80 |
-
print("User
|
| 81 |
return "Please Login to Hugging Face with the button.", None
|
| 82 |
|
| 83 |
api_url = DEFAULT_API_URL
|
| 84 |
questions_url = f"{api_url}/questions"
|
| 85 |
submit_url = f"{api_url}/submit"
|
| 86 |
|
| 87 |
-
# 1. Instantiate Agent
|
| 88 |
try:
|
| 89 |
agent = BasicAgent()
|
| 90 |
except Exception as e:
|
| 91 |
print(f"Error instantiating agent: {e}")
|
| 92 |
return f"Error initializing agent: {e}", None
|
| 93 |
-
|
| 94 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 95 |
print(agent_code)
|
| 96 |
|
|
@@ -101,16 +94,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 101 |
response.raise_for_status()
|
| 102 |
questions_data = response.json()
|
| 103 |
if not questions_data:
|
| 104 |
-
|
| 105 |
-
|
| 106 |
print(f"Fetched {len(questions_data)} questions.")
|
| 107 |
except requests.exceptions.RequestException as e:
|
| 108 |
print(f"Error fetching questions: {e}")
|
| 109 |
return f"Error fetching questions: {e}", None
|
| 110 |
except requests.exceptions.JSONDecodeError as e:
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
except Exception as e:
|
| 115 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 116 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
from transformers import pipeline
|
| 6 |
|
|
|
|
| 7 |
# --- Constants ---
|
| 8 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 9 |
|
| 10 |
# --- Basic Agent Definition ---
|
|
|
|
|
|
|
|
|
|
| 11 |
class BasicAgent:
|
| 12 |
def __init__(self):
|
| 13 |
print("Loading FLAN-T5 base model...")
|
| 14 |
self.pipeline = pipeline(
|
| 15 |
"text2text-generation",
|
| 16 |
+
model="google/flan-t5-base",
|
| 17 |
max_new_tokens=128,
|
| 18 |
temperature=0.3
|
| 19 |
)
|
|
|
|
| 29 |
return self.reverse_sentence(question)
|
| 30 |
else:
|
| 31 |
return self.model_response(question)
|
| 32 |
+
|
| 33 |
+
def model_response(self, question: str) -> str:
|
| 34 |
prompt = f"Please answer step by step:\n{question}"
|
| 35 |
output = self.pipeline(prompt)[0]["generated_text"]
|
| 36 |
return output.strip().split("Answer:")[-1].strip()
|
|
|
|
| 44 |
return f"Could not find info: {e}"
|
| 45 |
|
| 46 |
def parse_excel(self, task_id: str) -> str:
|
|
|
|
|
|
|
| 47 |
try:
|
| 48 |
file_url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
| 49 |
df = pd.read_excel(file_url)
|
| 50 |
+
# Example logic: sum sales for food category
|
| 51 |
food_sales = df[df['category'] == 'food']['sales'].sum()
|
| 52 |
return f"${food_sales:.2f}"
|
| 53 |
except Exception as e:
|
|
|
|
| 59 |
return reversed_text
|
| 60 |
|
| 61 |
|
| 62 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 63 |
"""
|
| 64 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 65 |
and displays the results.
|
| 66 |
"""
|
| 67 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
|
|
|
| 68 |
|
| 69 |
if profile:
|
| 70 |
+
username = f"{profile.username}"
|
| 71 |
+
print(f"User logged in: {username}")
|
| 72 |
else:
|
| 73 |
+
print("User not logged in.")
|
| 74 |
return "Please Login to Hugging Face with the button.", None
|
| 75 |
|
| 76 |
api_url = DEFAULT_API_URL
|
| 77 |
questions_url = f"{api_url}/questions"
|
| 78 |
submit_url = f"{api_url}/submit"
|
| 79 |
|
| 80 |
+
# 1. Instantiate Agent
|
| 81 |
try:
|
| 82 |
agent = BasicAgent()
|
| 83 |
except Exception as e:
|
| 84 |
print(f"Error instantiating agent: {e}")
|
| 85 |
return f"Error initializing agent: {e}", None
|
| 86 |
+
|
| 87 |
agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
|
| 88 |
print(agent_code)
|
| 89 |
|
|
|
|
| 94 |
response.raise_for_status()
|
| 95 |
questions_data = response.json()
|
| 96 |
if not questions_data:
|
| 97 |
+
print("Fetched questions list is empty.")
|
| 98 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 99 |
print(f"Fetched {len(questions_data)} questions.")
|
| 100 |
except requests.exceptions.RequestException as e:
|
| 101 |
print(f"Error fetching questions: {e}")
|
| 102 |
return f"Error fetching questions: {e}", None
|
| 103 |
except requests.exceptions.JSONDecodeError as e:
|
| 104 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 105 |
+
print(f"Response text: {response.text[:500]}")
|
| 106 |
+
return f"Error decoding server response for questions: {e}", None
|
| 107 |
except Exception as e:
|
| 108 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 109 |
return f"An unexpected error occurred fetching questions: {e}", None
|