Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,9 @@ from datetime import datetime
|
|
| 5 |
# Setup log file path
|
| 6 |
log_path = os.path.join(os.getcwd(), f"agent_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
# Configure logging
|
| 9 |
logging.basicConfig(
|
| 10 |
filename=log_path,
|
|
@@ -13,10 +16,9 @@ logging.basicConfig(
|
|
| 13 |
format='%(asctime)s | %(levelname)s | %(message)s',
|
| 14 |
)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
import requests
|
| 22 |
import pandas as pd
|
|
@@ -27,35 +29,47 @@ from PIL import Image
|
|
| 27 |
import soundfile as sf
|
| 28 |
|
| 29 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 30 |
|
| 31 |
class SmartAgent:
|
| 32 |
def __init__(self):
|
|
|
|
|
|
|
| 33 |
try:
|
| 34 |
self.text_gen = pipeline("text-generation", model="sshleifer/tiny-gpt2", device=-1)
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
logging.exception(f"β Text model load failed: {e}")
|
| 37 |
self.text_gen = None
|
| 38 |
|
| 39 |
try:
|
| 40 |
self.image_cls = pipeline("image-classification", model="google/vit-base-patch16-224", device=-1)
|
|
|
|
|
|
|
| 41 |
except Exception as e:
|
| 42 |
logging.exception(f"β Image model load failed: {e}")
|
| 43 |
self.image_cls = None
|
| 44 |
|
| 45 |
try:
|
| 46 |
self.audio_stt = pipeline("automatic-speech-recognition", model="openai/whisper-tiny", device=-1)
|
|
|
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
logging.exception(f"β Audio model load failed: {e}")
|
| 49 |
self.audio_stt = None
|
| 50 |
|
| 51 |
def get_file_for_task(self, task_id):
|
| 52 |
try:
|
| 53 |
-
resp = requests.get(f"{API_URL}/files/{task_id}", timeout=
|
| 54 |
if resp.status_code == 200:
|
| 55 |
path = os.path.join(tempfile.gettempdir(), f"{task_id}.bin")
|
| 56 |
with open(path, "wb") as f:
|
| 57 |
f.write(resp.content)
|
|
|
|
| 58 |
return path
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
logging.exception(f"β Download error: {e}")
|
| 61 |
return None
|
|
@@ -63,65 +77,93 @@ class SmartAgent:
|
|
| 63 |
def __call__(self, data):
|
| 64 |
qtype = data.get("type", "text")
|
| 65 |
question = data.get("question", "")
|
|
|
|
| 66 |
|
| 67 |
if qtype == "text" and self.text_gen:
|
| 68 |
try:
|
| 69 |
prompt = f"Answer the following question: {question}"
|
| 70 |
result = self.text_gen(prompt, max_new_tokens=20)
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
-
logging.exception(f"β Text error: {e}")
|
| 74 |
return "ERROR"
|
| 75 |
|
| 76 |
elif qtype == "image" and self.image_cls:
|
| 77 |
-
fp = self.get_file_for_task(
|
| 78 |
if not fp:
|
| 79 |
return "SKIPPED"
|
| 80 |
try:
|
| 81 |
with Image.open(fp) as img:
|
| 82 |
img = img.convert("RGB")
|
| 83 |
results = self.image_cls(img)
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
-
logging.exception(f"β Image error: {e}")
|
| 87 |
return "ERROR"
|
| 88 |
finally:
|
| 89 |
-
os.
|
|
|
|
| 90 |
|
| 91 |
elif qtype == "audio" and self.audio_stt:
|
| 92 |
-
fp = self.get_file_for_task(
|
| 93 |
if not fp:
|
| 94 |
return "SKIPPED"
|
| 95 |
try:
|
| 96 |
audio_data, rate = sf.read(fp)
|
| 97 |
result = self.audio_stt({"array": audio_data, "sampling_rate": rate})
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
except Exception as e:
|
| 100 |
-
logging.exception(f"β Audio error: {e}")
|
| 101 |
return "ERROR"
|
| 102 |
finally:
|
| 103 |
-
os.
|
|
|
|
| 104 |
|
|
|
|
| 105 |
return "SKIPPED"
|
| 106 |
|
| 107 |
def run_and_submit(username):
|
| 108 |
def gen():
|
| 109 |
-
yield f"β
Logged in as {username}", None
|
| 110 |
|
| 111 |
agent = SmartAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
try:
|
| 113 |
-
resp = requests.get(f"{API_URL}/questions", timeout=
|
|
|
|
| 114 |
questions = resp.json()
|
| 115 |
except Exception as e:
|
| 116 |
-
yield f"β Error fetching questions: {e}", None
|
| 117 |
return
|
| 118 |
|
| 119 |
answers, logs = [], []
|
|
|
|
| 120 |
|
| 121 |
-
logging.info(f"β
{
|
|
|
|
| 122 |
|
| 123 |
for i, q in enumerate(questions, 1):
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
try:
|
| 126 |
a = agent(q)
|
| 127 |
logging.debug(f"Q{i} Answer: {a}")
|
|
@@ -131,11 +173,16 @@ def run_and_submit(username):
|
|
| 131 |
|
| 132 |
answers.append({"task_id": q.get("task_id", "UNKNOWN"), "submitted_answer": a})
|
| 133 |
logs.append({
|
| 134 |
-
"Q":
|
| 135 |
-
"
|
| 136 |
-
"
|
|
|
|
| 137 |
})
|
|
|
|
|
|
|
|
|
|
| 138 |
|
|
|
|
| 139 |
|
| 140 |
try:
|
| 141 |
space_id = "dmfelder/DF_Final_Assignment_Template"
|
|
@@ -144,13 +191,17 @@ def run_and_submit(username):
|
|
| 144 |
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
| 145 |
"answers": answers
|
| 146 |
}
|
| 147 |
-
r = requests.post(f"{API_URL}/submit", json=body, timeout=
|
|
|
|
| 148 |
result = r.json()
|
| 149 |
score = result.get("score", "N/A")
|
| 150 |
-
msg = f"π Score: {score}%"
|
| 151 |
-
|
|
|
|
| 152 |
except Exception as e:
|
| 153 |
-
|
|
|
|
|
|
|
| 154 |
|
| 155 |
return gen()
|
| 156 |
|
|
@@ -159,30 +210,36 @@ with gr.Blocks(title="Final Agent") as app:
|
|
| 159 |
|
| 160 |
login = gr.LoginButton()
|
| 161 |
run = gr.Button("Run Agent")
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
log_file = gr.File(label="Download Log")
|
| 165 |
|
| 166 |
-
|
| 167 |
def start(profile):
|
| 168 |
if not profile:
|
| 169 |
-
yield "β You must log in first.", pd.DataFrame(), None
|
| 170 |
return
|
| 171 |
|
| 172 |
updates = []
|
| 173 |
final_df = None
|
| 174 |
|
| 175 |
-
for msg, df in run_and_submit(profile):
|
| 176 |
updates.append(msg)
|
| 177 |
if df is not None:
|
| 178 |
final_df = df
|
| 179 |
-
yield "\n".join(updates), final_df if final_df is not None else pd.DataFrame(), None
|
| 180 |
|
| 181 |
-
#
|
| 182 |
-
|
| 183 |
-
yield "\n".join(updates), final_df if final_df is not None else pd.DataFrame(), log_path
|
| 184 |
|
| 185 |
-
run.click(start, inputs=[login], outputs=[status, results, log_file])
|
| 186 |
|
| 187 |
if __name__ == "__main__":
|
| 188 |
-
app.launch()
|
|
|
|
| 5 |
# Setup log file path
|
| 6 |
log_path = os.path.join(os.getcwd(), f"agent_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log")
|
| 7 |
|
| 8 |
+
# Print log location for easy manual access
|
| 9 |
+
print(f"[INFO] Log file at: {log_path}")
|
| 10 |
+
|
| 11 |
# Configure logging
|
| 12 |
logging.basicConfig(
|
| 13 |
filename=log_path,
|
|
|
|
| 16 |
format='%(asctime)s | %(levelname)s | %(message)s',
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# Log application startup
|
| 20 |
+
logging.info(f"===== Application Startup at {datetime.now().isoformat()} =====")
|
| 21 |
+
logging.info(f"π Log file created at: {log_path}")
|
|
|
|
| 22 |
|
| 23 |
import requests
|
| 24 |
import pandas as pd
|
|
|
|
| 29 |
import soundfile as sf
|
| 30 |
|
| 31 |
API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 32 |
+
DEFAULT_TIMEOUT = 30 # seconds - adjust if HuggingFace latency changes
|
| 33 |
|
| 34 |
class SmartAgent:
|
| 35 |
def __init__(self):
|
| 36 |
+
self.models_loaded = {"text": False, "image": False, "audio": False}
|
| 37 |
+
|
| 38 |
try:
|
| 39 |
self.text_gen = pipeline("text-generation", model="sshleifer/tiny-gpt2", device=-1)
|
| 40 |
+
self.models_loaded["text"] = True
|
| 41 |
+
logging.info("β
Text generation model loaded successfully")
|
| 42 |
except Exception as e:
|
| 43 |
logging.exception(f"β Text model load failed: {e}")
|
| 44 |
self.text_gen = None
|
| 45 |
|
| 46 |
try:
|
| 47 |
self.image_cls = pipeline("image-classification", model="google/vit-base-patch16-224", device=-1)
|
| 48 |
+
self.models_loaded["image"] = True
|
| 49 |
+
logging.info("β
Image classification model loaded successfully")
|
| 50 |
except Exception as e:
|
| 51 |
logging.exception(f"β Image model load failed: {e}")
|
| 52 |
self.image_cls = None
|
| 53 |
|
| 54 |
try:
|
| 55 |
self.audio_stt = pipeline("automatic-speech-recognition", model="openai/whisper-tiny", device=-1)
|
| 56 |
+
self.models_loaded["audio"] = True
|
| 57 |
+
logging.info("β
Audio speech-to-text model loaded successfully")
|
| 58 |
except Exception as e:
|
| 59 |
logging.exception(f"β Audio model load failed: {e}")
|
| 60 |
self.audio_stt = None
|
| 61 |
|
| 62 |
def get_file_for_task(self, task_id):
|
| 63 |
try:
|
| 64 |
+
resp = requests.get(f"{API_URL}/files/{task_id}", timeout=DEFAULT_TIMEOUT)
|
| 65 |
if resp.status_code == 200:
|
| 66 |
path = os.path.join(tempfile.gettempdir(), f"{task_id}.bin")
|
| 67 |
with open(path, "wb") as f:
|
| 68 |
f.write(resp.content)
|
| 69 |
+
logging.debug(f"π File downloaded for task {task_id}")
|
| 70 |
return path
|
| 71 |
+
else:
|
| 72 |
+
logging.error(f"β File download failed with status {resp.status_code}")
|
| 73 |
except Exception as e:
|
| 74 |
logging.exception(f"β Download error: {e}")
|
| 75 |
return None
|
|
|
|
| 77 |
def __call__(self, data):
|
| 78 |
qtype = data.get("type", "text")
|
| 79 |
question = data.get("question", "")
|
| 80 |
+
task_id = data.get("task_id", "UNKNOWN")
|
| 81 |
|
| 82 |
if qtype == "text" and self.text_gen:
|
| 83 |
try:
|
| 84 |
prompt = f"Answer the following question: {question}"
|
| 85 |
result = self.text_gen(prompt, max_new_tokens=20)
|
| 86 |
+
answer = result[0]["generated_text"].split(":")[-1].strip()
|
| 87 |
+
logging.debug(f"π Text answer for {task_id}: {answer}")
|
| 88 |
+
return answer
|
| 89 |
except Exception as e:
|
| 90 |
+
logging.exception(f"β Text processing error for {task_id}: {e}")
|
| 91 |
return "ERROR"
|
| 92 |
|
| 93 |
elif qtype == "image" and self.image_cls:
|
| 94 |
+
fp = self.get_file_for_task(task_id)
|
| 95 |
if not fp:
|
| 96 |
return "SKIPPED"
|
| 97 |
try:
|
| 98 |
with Image.open(fp) as img:
|
| 99 |
img = img.convert("RGB")
|
| 100 |
results = self.image_cls(img)
|
| 101 |
+
answer = results[0]["label"]
|
| 102 |
+
logging.debug(f"πΌοΈ Image classification for {task_id}: {answer}")
|
| 103 |
+
return answer
|
| 104 |
except Exception as e:
|
| 105 |
+
logging.exception(f"β Image processing error for {task_id}: {e}")
|
| 106 |
return "ERROR"
|
| 107 |
finally:
|
| 108 |
+
if os.path.exists(fp):
|
| 109 |
+
os.remove(fp)
|
| 110 |
|
| 111 |
elif qtype == "audio" and self.audio_stt:
|
| 112 |
+
fp = self.get_file_for_task(task_id)
|
| 113 |
if not fp:
|
| 114 |
return "SKIPPED"
|
| 115 |
try:
|
| 116 |
audio_data, rate = sf.read(fp)
|
| 117 |
result = self.audio_stt({"array": audio_data, "sampling_rate": rate})
|
| 118 |
+
answer = result.get("text", "ERROR")
|
| 119 |
+
logging.debug(f"π΅ Audio transcription for {task_id}: {answer}")
|
| 120 |
+
return answer
|
| 121 |
except Exception as e:
|
| 122 |
+
logging.exception(f"β Audio processing error for {task_id}: {e}")
|
| 123 |
return "ERROR"
|
| 124 |
finally:
|
| 125 |
+
if os.path.exists(fp):
|
| 126 |
+
os.remove(fp)
|
| 127 |
|
| 128 |
+
logging.warning(f"β οΈ Skipping {qtype} task {task_id} - model not available")
|
| 129 |
return "SKIPPED"
|
| 130 |
|
| 131 |
def run_and_submit(username):
|
| 132 |
def gen():
|
| 133 |
+
yield f"β
Logged in as {username}", "Initializing...", "", "", None
|
| 134 |
|
| 135 |
agent = SmartAgent()
|
| 136 |
+
|
| 137 |
+
# Report model loading status
|
| 138 |
+
loaded_models = [k for k, v in agent.models_loaded.items() if v]
|
| 139 |
+
if loaded_models:
|
| 140 |
+
yield f"π€ Models loaded: {', '.join(loaded_models)}", f"Models ready: {', '.join(loaded_models)}", "", "", None
|
| 141 |
+
else:
|
| 142 |
+
yield "β No models loaded successfully", "No models available", "", "", None
|
| 143 |
+
return
|
| 144 |
+
|
| 145 |
try:
|
| 146 |
+
resp = requests.get(f"{API_URL}/questions", timeout=DEFAULT_TIMEOUT)
|
| 147 |
+
resp.raise_for_status()
|
| 148 |
questions = resp.json()
|
| 149 |
except Exception as e:
|
| 150 |
+
yield f"β Error fetching questions: {e}", "Failed to fetch questions", "", "", None
|
| 151 |
return
|
| 152 |
|
| 153 |
answers, logs = [], []
|
| 154 |
+
total_questions = len(questions)
|
| 155 |
|
| 156 |
+
logging.info(f"β
{total_questions} questions fetched")
|
| 157 |
+
yield f"π Processing {total_questions} questions...", f"0/{total_questions} questions processed", "", "", None
|
| 158 |
|
| 159 |
for i, q in enumerate(questions, 1):
|
| 160 |
+
question_preview = q.get('question', 'UNKNOWN')[:60]
|
| 161 |
+
full_question = q.get('question', 'UNKNOWN')
|
| 162 |
+
qtype = q.get('type', 'UNKNOWN')
|
| 163 |
+
|
| 164 |
+
logging.info(f"π¦ Q{i}/{total_questions}: {question_preview}")
|
| 165 |
+
yield f"π¦ Processing question {i}/{total_questions}: {question_preview}...", f"{i-1}/{total_questions} questions processed", f"Q{i} [{qtype}]: {full_question}", "Processing...", pd.DataFrame(logs)
|
| 166 |
+
|
| 167 |
try:
|
| 168 |
a = agent(q)
|
| 169 |
logging.debug(f"Q{i} Answer: {a}")
|
|
|
|
| 173 |
|
| 174 |
answers.append({"task_id": q.get("task_id", "UNKNOWN"), "submitted_answer": a})
|
| 175 |
logs.append({
|
| 176 |
+
"Q": i,
|
| 177 |
+
"Question": question_preview,
|
| 178 |
+
"Type": qtype,
|
| 179 |
+
"Answer": a or "SKIPPED"
|
| 180 |
})
|
| 181 |
+
|
| 182 |
+
# Update with completed question
|
| 183 |
+
yield f"π¦ Completed question {i}/{total_questions}", f"{i}/{total_questions} questions processed", f"Q{i} [{qtype}]: {full_question}", a or "SKIPPED", pd.DataFrame(logs)
|
| 184 |
|
| 185 |
+
yield f"π€ Submitting {len(answers)} answers...", f"{total_questions}/{total_questions} questions processed", "Submitting results...", "", pd.DataFrame(logs)
|
| 186 |
|
| 187 |
try:
|
| 188 |
space_id = "dmfelder/DF_Final_Assignment_Template"
|
|
|
|
| 191 |
"agent_code": f"https://huggingface.co/spaces/{space_id}/tree/main",
|
| 192 |
"answers": answers
|
| 193 |
}
|
| 194 |
+
r = requests.post(f"{API_URL}/submit", json=body, timeout=DEFAULT_TIMEOUT * 2) # Double timeout for submission
|
| 195 |
+
r.raise_for_status()
|
| 196 |
result = r.json()
|
| 197 |
score = result.get("score", "N/A")
|
| 198 |
+
msg = f"π Final Score: {score}%"
|
| 199 |
+
logging.info(f"β
Submission successful: {score}%")
|
| 200 |
+
yield msg, f"Complete! Score: {score}%", "Evaluation complete", f"Final score: {score}%", pd.DataFrame(logs)
|
| 201 |
except Exception as e:
|
| 202 |
+
error_msg = f"β Submit error: {e}"
|
| 203 |
+
logging.exception(error_msg)
|
| 204 |
+
yield error_msg, "Submission failed", "Error occurred", str(e), pd.DataFrame(logs)
|
| 205 |
|
| 206 |
return gen()
|
| 207 |
|
|
|
|
| 210 |
|
| 211 |
login = gr.LoginButton()
|
| 212 |
run = gr.Button("Run Agent")
|
| 213 |
+
|
| 214 |
+
with gr.Row():
|
| 215 |
+
with gr.Column():
|
| 216 |
+
status = gr.Textbox(lines=10, label="Status")
|
| 217 |
+
progress = gr.Textbox(label="Progress", value="Ready to start...")
|
| 218 |
+
with gr.Column():
|
| 219 |
+
current_question = gr.Textbox(lines=3, label="Current Question", value="")
|
| 220 |
+
current_answer = gr.Textbox(label="Current Answer", value="")
|
| 221 |
+
|
| 222 |
+
results = gr.DataFrame(label="All Results")
|
| 223 |
log_file = gr.File(label="Download Log")
|
| 224 |
|
|
|
|
| 225 |
def start(profile):
|
| 226 |
if not profile:
|
| 227 |
+
yield "β You must log in first.", "Not logged in", "", "", pd.DataFrame(), None
|
| 228 |
return
|
| 229 |
|
| 230 |
updates = []
|
| 231 |
final_df = None
|
| 232 |
|
| 233 |
+
for msg, prog, question, answer, df in run_and_submit(profile):
|
| 234 |
updates.append(msg)
|
| 235 |
if df is not None:
|
| 236 |
final_df = df
|
| 237 |
+
yield "\n".join(updates), prog, question, answer, final_df if final_df is not None else pd.DataFrame(), None
|
| 238 |
|
| 239 |
+
# Final yield includes log file path
|
| 240 |
+
yield "\n".join(updates), prog, question, answer, final_df if final_df is not None else pd.DataFrame(), log_path
|
|
|
|
| 241 |
|
| 242 |
+
run.click(start, inputs=[login], outputs=[status, progress, current_question, current_answer, results, log_file])
|
| 243 |
|
| 244 |
if __name__ == "__main__":
|
| 245 |
+
app.launch()
|