Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import os
|
|
|
2 |
import json
|
3 |
import httpx
|
4 |
import gradio as gr
|
@@ -55,9 +56,9 @@ def refine_response(user_input):
|
|
55 |
deepseek_response = query_deepseek(user_input)
|
56 |
|
57 |
# If either response failed, return the available one
|
58 |
-
if
|
59 |
return f"Only DeepSeek Response:\n{deepseek_response}"
|
60 |
-
if
|
61 |
return f"Only Gemma Response:\n{gemma_response}"
|
62 |
|
63 |
# Prepare refinement prompt
|
@@ -69,27 +70,46 @@ def refine_response(user_input):
|
|
69 |
|
70 |
Please combine the best elements of both, improve clarity, and provide a final refined answer.
|
71 |
"""
|
72 |
-
|
73 |
-
# Send request to OpenRouter
|
74 |
-
response = httpx.post(
|
75 |
-
"https://openrouter.ai/api/v1/chat/completions",
|
76 |
-
headers={
|
77 |
-
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
78 |
-
"Content-Type": "application/json"
|
79 |
-
},
|
80 |
-
json={
|
81 |
-
"model": "deepseek/deepseek-r1:free",
|
82 |
-
"messages": [{"role": "user", "content": improvement_prompt}]
|
83 |
-
}
|
84 |
-
)
|
85 |
|
86 |
-
#
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
-
#
|
90 |
-
|
91 |
-
|
92 |
-
return response_json["choices"][0]["message"]["content"]
|
93 |
|
94 |
except Exception as e:
|
95 |
return f"Error refining response: {str(e)}"
|
|
|
1 |
import os
|
2 |
+
import time
|
3 |
import json
|
4 |
import httpx
|
5 |
import gradio as gr
|
|
|
56 |
deepseek_response = query_deepseek(user_input)
|
57 |
|
58 |
# If either response failed, return the available one
|
59 |
+
if not gemma_response.strip():
|
60 |
return f"Only DeepSeek Response:\n{deepseek_response}"
|
61 |
+
if not deepseek_response.strip():
|
62 |
return f"Only Gemma Response:\n{gemma_response}"
|
63 |
|
64 |
# Prepare refinement prompt
|
|
|
70 |
|
71 |
Please combine the best elements of both, improve clarity, and provide a final refined answer.
|
72 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
+
# Retry loop for OpenRouter API
|
75 |
+
max_retries = 3
|
76 |
+
for attempt in range(max_retries):
|
77 |
+
try:
|
78 |
+
response = httpx.post(
|
79 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
80 |
+
headers={
|
81 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
82 |
+
"Content-Type": "application/json"
|
83 |
+
},
|
84 |
+
json={
|
85 |
+
"model": "deepseek/deepseek-r1:free",
|
86 |
+
"messages": [{"role": "user", "content": improvement_prompt}]
|
87 |
+
},
|
88 |
+
timeout=30 # Increase timeout to 30 seconds
|
89 |
+
)
|
90 |
+
|
91 |
+
# Debugging: Print response
|
92 |
+
print(f"Attempt {attempt + 1}: OpenRouter Response:", response.text)
|
93 |
+
|
94 |
+
# Check if the response is valid JSON
|
95 |
+
response_json = response.json()
|
96 |
+
|
97 |
+
# Extract the refined response
|
98 |
+
refined_content = response_json["choices"][0]["message"]["content"]
|
99 |
+
|
100 |
+
# If DeepSeek gives an empty response, retry
|
101 |
+
if refined_content.strip():
|
102 |
+
return refined_content
|
103 |
+
else:
|
104 |
+
print("Received empty response from DeepSeek, retrying...")
|
105 |
+
time.sleep(2) # Wait before retrying
|
106 |
+
|
107 |
+
except Exception as e:
|
108 |
+
print(f"Error on attempt {attempt + 1}: {str(e)}")
|
109 |
+
time.sleep(2) # Wait before retrying
|
110 |
|
111 |
+
# If all retries fail, return the better of the two initial responses
|
112 |
+
return f"Refinement failed. Here’s the best available response:\n\n{max(gemma_response, deepseek_response, key=len)}"
|
|
|
|
|
113 |
|
114 |
except Exception as e:
|
115 |
return f"Error refining response: {str(e)}"
|