Update app.py
Browse files
app.py
CHANGED
|
@@ -81,6 +81,10 @@ For example, if asked "What is the capital of France?", respond simply with "Par
|
|
| 81 |
try:
|
| 82 |
raw_response = self.agent.run([{"role": "user", "content": full_prompt}])
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
# Handle ALL possible response formats
|
| 85 |
if isinstance(raw_response, dict):
|
| 86 |
answer = raw_response.get('choices', [{}])[0].get('message', {}).get('content', str(raw_response))
|
|
@@ -106,8 +110,13 @@ For example, if asked "What is the capital of France?", respond simply with "Par
|
|
| 106 |
else:
|
| 107 |
answer = str(raw_response)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
except Exception as agent_error:
|
| 110 |
print(f"Agent run error: {agent_error}")
|
|
|
|
| 111 |
return f"Agent error: {agent_error}"
|
| 112 |
|
| 113 |
# Clean the answer
|
|
@@ -129,30 +138,50 @@ For example, if asked "What is the capital of France?", respond simply with "Par
|
|
| 129 |
"""
|
| 130 |
Ultra-safe answer extraction and cleaning
|
| 131 |
"""
|
| 132 |
-
# Force to string immediately
|
| 133 |
-
|
| 134 |
-
answer
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
# Remove wrapping quotes
|
| 151 |
-
if (answer.startswith('"') and answer.endswith('"')) or \
|
| 152 |
-
(answer.startswith("'") and answer.endswith("'")):
|
| 153 |
-
answer = answer[1:-1].strip()
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 158 |
"""
|
|
|
|
| 81 |
try:
|
| 82 |
raw_response = self.agent.run([{"role": "user", "content": full_prompt}])
|
| 83 |
|
| 84 |
+
if self.verbose:
|
| 85 |
+
print(f"Raw response type: {type(raw_response)}")
|
| 86 |
+
print(f"Raw response: {raw_response}")
|
| 87 |
+
|
| 88 |
# Handle ALL possible response formats
|
| 89 |
if isinstance(raw_response, dict):
|
| 90 |
answer = raw_response.get('choices', [{}])[0].get('message', {}).get('content', str(raw_response))
|
|
|
|
| 110 |
else:
|
| 111 |
answer = str(raw_response)
|
| 112 |
|
| 113 |
+
if self.verbose:
|
| 114 |
+
print(f"Extracted answer type: {type(answer)}")
|
| 115 |
+
print(f"Extracted answer value: {answer}")
|
| 116 |
+
|
| 117 |
except Exception as agent_error:
|
| 118 |
print(f"Agent run error: {agent_error}")
|
| 119 |
+
traceback.print_exc()
|
| 120 |
return f"Agent error: {agent_error}"
|
| 121 |
|
| 122 |
# Clean the answer
|
|
|
|
| 138 |
"""
|
| 139 |
Ultra-safe answer extraction and cleaning
|
| 140 |
"""
|
| 141 |
+
# Force to string immediately with extra safety
|
| 142 |
+
try:
|
| 143 |
+
if answer is None:
|
| 144 |
+
return ""
|
| 145 |
+
if isinstance(answer, list):
|
| 146 |
+
# If it's a list, try to extract meaningful content
|
| 147 |
+
if len(answer) == 0:
|
| 148 |
+
return ""
|
| 149 |
+
# Try to get content from first element
|
| 150 |
+
answer = answer[0] if len(answer) > 0 else ""
|
| 151 |
+
if isinstance(answer, dict):
|
| 152 |
+
# If it's a dict, try to get 'content' or convert to string
|
| 153 |
+
answer = answer.get('content', str(answer))
|
| 154 |
+
if not isinstance(answer, str):
|
| 155 |
+
answer = str(answer)
|
| 156 |
+
except Exception as e:
|
| 157 |
+
print(f"Error in initial conversion: {e}")
|
| 158 |
+
return str(answer) if answer else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
# Now answer should definitely be a string
|
| 161 |
+
try:
|
| 162 |
+
# Strip whitespace
|
| 163 |
+
answer = answer.strip()
|
| 164 |
+
|
| 165 |
+
# Remove common prefixes that models often add
|
| 166 |
+
prefixes_to_remove = [
|
| 167 |
+
"The answer is ", "Answer: ", "Final answer: ", "The result is ",
|
| 168 |
+
"To answer this question: ", "Based on the information provided, ",
|
| 169 |
+
"According to the information: ",
|
| 170 |
+
]
|
| 171 |
+
|
| 172 |
+
for prefix in prefixes_to_remove:
|
| 173 |
+
if answer.startswith(prefix):
|
| 174 |
+
answer = answer[len(prefix):].strip()
|
| 175 |
+
|
| 176 |
+
# Remove wrapping quotes
|
| 177 |
+
if (answer.startswith('"') and answer.endswith('"')) or \
|
| 178 |
+
(answer.startswith("'") and answer.endswith("'")):
|
| 179 |
+
answer = answer[1:-1].strip()
|
| 180 |
+
|
| 181 |
+
return answer
|
| 182 |
+
except Exception as e:
|
| 183 |
+
print(f"Error in answer cleaning: {e}, returning raw string")
|
| 184 |
+
return str(answer) if answer else ""
|
| 185 |
|
| 186 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 187 |
"""
|