Spaces:
Sleeping
Sleeping
Update app.py
#2
by olcapone - opened
1 unmodified line2345678101 unmodified lines11011111211311411511611711211311411511611711811912012112211811912012112212312412512612712812913013113213313413513613713813914014114214314414514671 unmodified lines2182192202212222232242252262271 unmodified lineimport gradio as grimport requestsimport pandas as pdimport timefrom smolagents import LiteLLMModel, CodeAgent, Tool
# --- Constants ---101 unmodified lines )
def __call__(self, question: str) -> str: max_retries = 3 retry_delay = 10 # Start with 10 seconds for attempt in range(max_retries): try: result = self.agent.run(question) # Extract only the final answer without any wrappers final_str = str(result).strip() # Remove any potential prefixes if final_str.startswith('[ANSWER]'): final_str = final_str[8:].strip() if final_str.startswith('Final answer:'): final_str = final_str[13:].strip() if final_str.startswith('Answer:'): final_str = final_str[7:].strip() return final_str result = self.agent.run(question) # Extract only the final answer without any wrappers final_str = str(result).strip() # Remove any potential prefixes if final_str.startswith('[ANSWER]'): final_str = final_str[8:].strip() if final_str.startswith('Final answer:'): final_str = final_str[13:].strip() if final_str.startswith('Answer:'): final_str = final_str[7:].strip() return final_str except Exception as e: # Check if it's a rate limit error if "RateLimitError" in str(e) or "rate_limit_exceeded" in str(e): if attempt < max_retries - 1: # Not the last attempt print(f"Rate limit hit. Waiting {retry_delay} seconds before retry {attempt + 1}/{max_retries}") time.sleep(retry_delay) retry_delay *= 2 # Exponential backoff continue else: return f"Rate limit error after {max_retries} attempts: {e}" else: # Not a rate limit error, re-raise raise e return f"Failed to get response after {max_retries} attempts"
# --- Main Application Functions ---def run_and_submit_all(profile: gr.OAuthProfile | None):71 unmodified lines submitted_answer = agent(question_text) answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer}) results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer}) # Add a small delay between questions to help with rate limiting if progress_count < total_questions: # Don't delay after the last question time.sleep(2) except Exception as e: print(f"Error running agent on task {task_id}: {e}") results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})app.py+36-11