Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,12 +23,21 @@ from model_suggestions import add_suggestion, get_suggestions_html
|
|
| 23 |
from release_notes import get_release_notes_html
|
| 24 |
|
| 25 |
|
| 26 |
-
#
|
| 27 |
logging.basicConfig(
|
| 28 |
level=logging.INFO,
|
| 29 |
-
format='%(asctime)s - %(levelname)s - %(message)s'
|
|
|
|
| 30 |
)
|
| 31 |
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Start the backup thread
|
| 34 |
start_backup_thread()
|
|
@@ -56,7 +65,7 @@ def call_ollama_api(model, prompt):
|
|
| 56 |
)
|
| 57 |
|
| 58 |
try:
|
| 59 |
-
logger.info(
|
| 60 |
response = client.chat.completions.create(
|
| 61 |
model=model,
|
| 62 |
messages=[
|
|
@@ -71,40 +80,56 @@ def call_ollama_api(model, prompt):
|
|
| 71 |
],
|
| 72 |
timeout=180
|
| 73 |
)
|
| 74 |
-
logger.info(
|
| 75 |
|
| 76 |
if not response or not response.choices:
|
| 77 |
-
logger.error(
|
| 78 |
-
return
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
content = response.choices[0].message.content
|
| 81 |
if not content:
|
| 82 |
-
logger.error(
|
| 83 |
-
return
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
#
|
| 89 |
import re
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
# Clean up any double newlines that might be left
|
| 93 |
-
content = re.sub(r'\n\s*\n', '\n', content.strip())
|
| 94 |
|
| 95 |
-
if
|
| 96 |
-
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
except requests.exceptions.Timeout:
|
| 103 |
-
logger.error(
|
| 104 |
-
return
|
|
|
|
|
|
|
|
|
|
| 105 |
except Exception as e:
|
| 106 |
-
logger.error(f"Error calling Ollama API
|
| 107 |
-
return
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
# Generate responses using two randomly selected models
|
| 110 |
def get_battle_counts():
|
|
@@ -117,7 +142,13 @@ def get_battle_counts():
|
|
| 117 |
def generate_responses(prompt):
|
| 118 |
available_models = get_available_models()
|
| 119 |
if len(available_models) < 2:
|
| 120 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
battle_counts = get_battle_counts()
|
| 123 |
|
|
@@ -142,16 +173,20 @@ def generate_responses(prompt):
|
|
| 142 |
# Update recent opponents
|
| 143 |
update_recent_opponents(model_a, model_b)
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
-
|
|
|
|
| 149 |
|
| 150 |
def battle_arena(prompt):
|
| 151 |
response_a, response_b, model_a, model_b = generate_responses(prompt)
|
| 152 |
|
| 153 |
# Check for API errors in responses
|
| 154 |
-
if "Error: Unable to get response from the model" in
|
|
|
|
|
|
|
| 155 |
return (
|
| 156 |
[], [], None, None,
|
| 157 |
gr.update(value=[]),
|
|
@@ -168,21 +203,12 @@ def battle_arena(prompt):
|
|
| 168 |
nickname_a = random.choice(config.model_nicknames)
|
| 169 |
nickname_b = random.choice(config.model_nicknames)
|
| 170 |
|
| 171 |
-
#
|
| 172 |
-
response_a_formatted = [
|
| 173 |
-
{"role": "user", "content": prompt},
|
| 174 |
-
{"role": "assistant", "content": response_a}
|
| 175 |
-
]
|
| 176 |
-
response_b_formatted = [
|
| 177 |
-
{"role": "user", "content": prompt},
|
| 178 |
-
{"role": "assistant", "content": response_b}
|
| 179 |
-
]
|
| 180 |
-
|
| 181 |
if random.choice([True, False]):
|
| 182 |
return (
|
| 183 |
-
|
| 184 |
-
gr.update(label=nickname_a, value=
|
| 185 |
-
gr.update(label=nickname_b, value=
|
| 186 |
gr.update(interactive=True, value=f"Vote for {nickname_a}"),
|
| 187 |
gr.update(interactive=True, value=f"Vote for {nickname_b}"),
|
| 188 |
gr.update(interactive=True, visible=True),
|
|
@@ -193,9 +219,9 @@ def battle_arena(prompt):
|
|
| 193 |
)
|
| 194 |
else:
|
| 195 |
return (
|
| 196 |
-
|
| 197 |
-
gr.update(label=nickname_a, value=
|
| 198 |
-
gr.update(label=nickname_b, value=
|
| 199 |
gr.update(interactive=True, value=f"Vote for {nickname_a}"),
|
| 200 |
gr.update(interactive=True, value=f"Vote for {nickname_b}"),
|
| 201 |
gr.update(interactive=True, visible=True),
|
|
|
|
| 23 |
from release_notes import get_release_notes_html
|
| 24 |
|
| 25 |
|
| 26 |
+
# Update the logging format to redact URLs
|
| 27 |
logging.basicConfig(
|
| 28 |
level=logging.INFO,
|
| 29 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 30 |
+
filters=[lambda s: s.replace(config.API_URL, '[REDACTED]') if config.API_URL in s else s]
|
| 31 |
)
|
| 32 |
logger = logging.getLogger(__name__)
|
| 33 |
+
# Add this at the top with other imports
|
| 34 |
+
class RedactURLsFilter(logging.Filter):
|
| 35 |
+
def filter(self, record):
|
| 36 |
+
record.msg = record.msg.replace(config.NEXTCLOUD_URL, '[CLOUD_STORAGE]')
|
| 37 |
+
return super().filter(record)
|
| 38 |
+
|
| 39 |
+
# Apply the filter to all loggers
|
| 40 |
+
logging.getLogger().addFilter(RedactURLsFilter())
|
| 41 |
|
| 42 |
# Start the backup thread
|
| 43 |
start_backup_thread()
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
try:
|
| 68 |
+
logger.info("Starting API call")
|
| 69 |
response = client.chat.completions.create(
|
| 70 |
model=model,
|
| 71 |
messages=[
|
|
|
|
| 80 |
],
|
| 81 |
timeout=180
|
| 82 |
)
|
| 83 |
+
logger.info("Received response")
|
| 84 |
|
| 85 |
if not response or not response.choices:
|
| 86 |
+
logger.error("Empty response received")
|
| 87 |
+
return [
|
| 88 |
+
{"role": "user", "content": prompt},
|
| 89 |
+
{"role": "assistant", "content": "Error: Empty response from the model"}
|
| 90 |
+
]
|
| 91 |
|
| 92 |
content = response.choices[0].message.content
|
| 93 |
if not content:
|
| 94 |
+
logger.error("Empty content received")
|
| 95 |
+
return [
|
| 96 |
+
{"role": "user", "content": prompt},
|
| 97 |
+
{"role": "assistant", "content": "Error: Empty content from the model"}
|
| 98 |
+
]
|
| 99 |
|
| 100 |
+
# Extract thinking part and main content using regex
|
| 101 |
import re
|
| 102 |
+
thinking_match = re.search(r'<think>(.*?)</think>', content, flags=re.DOTALL)
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
if thinking_match:
|
| 105 |
+
thinking_content = thinking_match.group(1).strip()
|
| 106 |
+
main_content = re.sub(r'<think>.*?</think>', '', content, flags=re.DOTALL).strip()
|
| 107 |
|
| 108 |
+
logger.info("Found thinking content, creating structured response")
|
| 109 |
+
return [
|
| 110 |
+
{"role": "user", "content": prompt},
|
| 111 |
+
{"role": "assistant", "content": f"{main_content}\n\n<details><summary>🤔 View thinking process</summary>\n\n{thinking_content}\n\n</details>"}
|
| 112 |
+
]
|
| 113 |
+
|
| 114 |
+
# If no thinking tags, return normal content
|
| 115 |
+
logger.info("No thinking tags found, returning normal content")
|
| 116 |
+
return [
|
| 117 |
+
{"role": "user", "content": prompt},
|
| 118 |
+
{"role": "assistant", "content": content.strip()}
|
| 119 |
+
]
|
| 120 |
|
| 121 |
except requests.exceptions.Timeout:
|
| 122 |
+
logger.error("Timeout error after 180 seconds")
|
| 123 |
+
return [
|
| 124 |
+
{"role": "user", "content": prompt},
|
| 125 |
+
{"role": "assistant", "content": "Error: Model response timed out after 180 seconds"}
|
| 126 |
+
]
|
| 127 |
except Exception as e:
|
| 128 |
+
logger.error(f"Error calling Ollama API: {str(e)}", exc_info=True)
|
| 129 |
+
return [
|
| 130 |
+
{"role": "user", "content": prompt},
|
| 131 |
+
{"role": "assistant", "content": f"Error: Unable to get response from the model. Error: {str(e)}"}
|
| 132 |
+
]
|
| 133 |
|
| 134 |
# Generate responses using two randomly selected models
|
| 135 |
def get_battle_counts():
|
|
|
|
| 142 |
def generate_responses(prompt):
|
| 143 |
available_models = get_available_models()
|
| 144 |
if len(available_models) < 2:
|
| 145 |
+
return [
|
| 146 |
+
{"role": "user", "content": prompt},
|
| 147 |
+
{"role": "assistant", "content": "Error: Not enough models available"}
|
| 148 |
+
], [
|
| 149 |
+
{"role": "user", "content": prompt},
|
| 150 |
+
{"role": "assistant", "content": "Error: Not enough models available"}
|
| 151 |
+
], None, None
|
| 152 |
|
| 153 |
battle_counts = get_battle_counts()
|
| 154 |
|
|
|
|
| 173 |
# Update recent opponents
|
| 174 |
update_recent_opponents(model_a, model_b)
|
| 175 |
|
| 176 |
+
# Get responses from both models
|
| 177 |
+
response_a = call_ollama_api(model_a, prompt)
|
| 178 |
+
response_b = call_ollama_api(model_b, prompt)
|
| 179 |
|
| 180 |
+
# Return responses directly (already formatted correctly)
|
| 181 |
+
return response_a, response_b, model_a, model_b
|
| 182 |
|
| 183 |
def battle_arena(prompt):
|
| 184 |
response_a, response_b, model_a, model_b = generate_responses(prompt)
|
| 185 |
|
| 186 |
# Check for API errors in responses
|
| 187 |
+
if any("Error: Unable to get response from the model" in msg["content"]
|
| 188 |
+
for msg in response_a + response_b
|
| 189 |
+
if msg["role"] == "assistant"):
|
| 190 |
return (
|
| 191 |
[], [], None, None,
|
| 192 |
gr.update(value=[]),
|
|
|
|
| 203 |
nickname_a = random.choice(config.model_nicknames)
|
| 204 |
nickname_b = random.choice(config.model_nicknames)
|
| 205 |
|
| 206 |
+
# The responses are already in the correct format, no need to reformat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
if random.choice([True, False]):
|
| 208 |
return (
|
| 209 |
+
response_a, response_b, model_a, model_b,
|
| 210 |
+
gr.update(label=nickname_a, value=response_a),
|
| 211 |
+
gr.update(label=nickname_b, value=response_b),
|
| 212 |
gr.update(interactive=True, value=f"Vote for {nickname_a}"),
|
| 213 |
gr.update(interactive=True, value=f"Vote for {nickname_b}"),
|
| 214 |
gr.update(interactive=True, visible=True),
|
|
|
|
| 219 |
)
|
| 220 |
else:
|
| 221 |
return (
|
| 222 |
+
response_b, response_a, model_b, model_a,
|
| 223 |
+
gr.update(label=nickname_a, value=response_b),
|
| 224 |
+
gr.update(label=nickname_b, value=response_a),
|
| 225 |
gr.update(interactive=True, value=f"Vote for {nickname_a}"),
|
| 226 |
gr.update(interactive=True, value=f"Vote for {nickname_b}"),
|
| 227 |
gr.update(interactive=True, visible=True),
|