Spaces:
Sleeping
Sleeping
fixing agent
Browse files
app.py
CHANGED
|
@@ -35,8 +35,11 @@ class BasicAgent:
|
|
| 35 |
raise e
|
| 36 |
|
| 37 |
def __call__(self, question: str) -> str:
|
| 38 |
-
print(f"Agent received question
|
|
|
|
| 39 |
try:
|
|
|
|
|
|
|
| 40 |
# Create a more detailed prompt for better responses
|
| 41 |
system_prompt = "You are a helpful AI assistant. Provide clear, accurate, and concise answers to questions."
|
| 42 |
|
|
@@ -56,14 +59,33 @@ class BasicAgent:
|
|
| 56 |
temperature=0.7,
|
| 57 |
)
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
return answer
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
-
error_msg = f"
|
| 65 |
-
print(
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 69 |
"""
|
|
@@ -94,10 +116,21 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
| 94 |
|
| 95 |
# 1. Instantiate Agent
|
| 96 |
try:
|
|
|
|
| 97 |
agent = BasicAgent()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
# 2. Fetch Questions
|
| 103 |
print(f"Fetching questions from: {questions_url}")
|
|
|
|
| 35 |
raise e
|
| 36 |
|
| 37 |
def __call__(self, question: str) -> str:
|
| 38 |
+
print(f"π€ Agent received question: {question[:100]}...")
|
| 39 |
+
|
| 40 |
try:
|
| 41 |
+
print("π Making API call to HF Inference...")
|
| 42 |
+
|
| 43 |
# Create a more detailed prompt for better responses
|
| 44 |
system_prompt = "You are a helpful AI assistant. Provide clear, accurate, and concise answers to questions."
|
| 45 |
|
|
|
|
| 59 |
temperature=0.7,
|
| 60 |
)
|
| 61 |
|
| 62 |
+
print("β
API call successful!")
|
| 63 |
+
answer = completion.choices[0].message.content.strip()
|
| 64 |
+
print(f"π Agent returning answer: {answer[:200]}...")
|
| 65 |
+
|
| 66 |
+
# Ensure we're not returning empty or None answers
|
| 67 |
+
if not answer or answer.lower().strip() == "":
|
| 68 |
+
return "I apologize, but I couldn't generate a proper response to your question."
|
| 69 |
+
|
| 70 |
return answer
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
+
error_msg = f"β HF API Error: {str(e)}"
|
| 74 |
+
print(error_msg)
|
| 75 |
+
print(f"π Full error details: {repr(e)}")
|
| 76 |
+
|
| 77 |
+
# Return a more informative error message
|
| 78 |
+
return f"Sorry, I encountered an error while processing your question: {str(e)}"
|
| 79 |
+
|
| 80 |
+
def test_connection(self):
|
| 81 |
+
"""Test if the HF API connection is working"""
|
| 82 |
+
try:
|
| 83 |
+
test_response = self("What is 2+2?")
|
| 84 |
+
print(f"π§ͺ Test response: {test_response}")
|
| 85 |
+
return True, test_response
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"π« Test failed: {e}")
|
| 88 |
+
return False, str(e)
|
| 89 |
|
| 90 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 91 |
"""
|
|
|
|
| 116 |
|
| 117 |
# 1. Instantiate Agent
|
| 118 |
try:
|
| 119 |
+
print("π Initializing BasicAgent...")
|
| 120 |
agent = BasicAgent()
|
| 121 |
+
|
| 122 |
+
# Test the agent before proceeding
|
| 123 |
+
print("π§ͺ Testing agent connection...")
|
| 124 |
+
test_success, test_result = agent.test_connection()
|
| 125 |
+
if not test_success:
|
| 126 |
+
return f"β Agent test failed: {test_result}\nPlease check your HF_TOKEN in environment variables.", None
|
| 127 |
+
|
| 128 |
+
print(f"β
Agent test successful: {test_result[:100]}...")
|
| 129 |
+
|
| 130 |
except Exception as e:
|
| 131 |
+
error_msg = f"β Error initializing agent: {e}"
|
| 132 |
+
print(error_msg)
|
| 133 |
+
return error_msg, None
|
| 134 |
|
| 135 |
# 2. Fetch Questions
|
| 136 |
print(f"Fetching questions from: {questions_url}")
|