Spaces:
Running
Running
Delete model_suggestions.py
Browse files- model_suggestions.py +0 -187
model_suggestions.py
DELETED
@@ -1,187 +0,0 @@
|
|
1 |
-
from nc_py_api import Nextcloud
|
2 |
-
import json
|
3 |
-
from typing import Dict
|
4 |
-
from datetime import datetime
|
5 |
-
import os
|
6 |
-
import re
|
7 |
-
import config
|
8 |
-
|
9 |
-
# Initialize Nextcloud client
|
10 |
-
nc = Nextcloud(
|
11 |
-
nextcloud_url=config.NEXTCLOUD_URL,
|
12 |
-
nc_auth_user=config.NEXTCLOUD_USERNAME,
|
13 |
-
nc_auth_pass=config.NEXTCLOUD_PASSWORD
|
14 |
-
)
|
15 |
-
|
16 |
-
def validate_model_url(url: str) -> bool:
|
17 |
-
"""
|
18 |
-
Validate if the provided URL matches the expected format.
|
19 |
-
Accepts both direct Ollama models and HuggingFace GGUF models.
|
20 |
-
"""
|
21 |
-
# Pattern for HuggingFace GGUF models
|
22 |
-
hf_pattern = r'^hf\.co/[\w-]+/[\w\.-]+(?:-GGUF)?:Q[0-9]+(?:_[A-Z0-9_]+)?$'
|
23 |
-
# Pattern for direct Ollama models
|
24 |
-
ollama_pattern = r'^[\w\.-]+(?::\d+(?:\.\d+)?[b])?(?:-[\w-]+)?:Q[0-9]+(?:_[A-Z0-9_]+)?$'
|
25 |
-
|
26 |
-
return bool(re.match(hf_pattern, url) or re.match(ollama_pattern, url))
|
27 |
-
|
28 |
-
def load_suggestions() -> Dict:
|
29 |
-
"""Load suggestions from Nextcloud with local file fallback."""
|
30 |
-
try:
|
31 |
-
# Try to load from Nextcloud
|
32 |
-
remote_data = nc.files.download(config.NEXTCLOUD_SUGGESTIONS_PATH)
|
33 |
-
if remote_data:
|
34 |
-
suggestions = json.loads(remote_data.decode('utf-8'))
|
35 |
-
# Update local cache
|
36 |
-
with open('model_suggestions.json', 'w') as f:
|
37 |
-
json.dump(suggestions, f, indent=2)
|
38 |
-
return suggestions
|
39 |
-
except Exception as e:
|
40 |
-
print(f"Could not load from Nextcloud: {e}")
|
41 |
-
|
42 |
-
# Try local cache
|
43 |
-
if os.path.exists('model_suggestions.json'):
|
44 |
-
try:
|
45 |
-
with open('model_suggestions.json', 'r') as f:
|
46 |
-
return json.load(f)
|
47 |
-
except Exception as e:
|
48 |
-
print(f"Could not load from local cache: {e}")
|
49 |
-
|
50 |
-
# Initialize new suggestions if both attempts fail
|
51 |
-
return {
|
52 |
-
"suggestions": {},
|
53 |
-
"last_updated": datetime.now().isoformat(),
|
54 |
-
"total_suggestions": 0
|
55 |
-
}
|
56 |
-
|
57 |
-
def save_suggestions(suggestions: Dict) -> bool:
|
58 |
-
"""Save suggestions to both Nextcloud and local cache."""
|
59 |
-
try:
|
60 |
-
# Update metadata
|
61 |
-
suggestions["last_updated"] = datetime.now().isoformat()
|
62 |
-
suggestions["total_suggestions"] = sum(s["count"] for s in suggestions["suggestions"].values())
|
63 |
-
|
64 |
-
# Save to Nextcloud
|
65 |
-
json_data = json.dumps(suggestions, indent=2)
|
66 |
-
nc.files.upload(config.NEXTCLOUD_SUGGESTIONS_PATH, json_data.encode('utf-8'))
|
67 |
-
|
68 |
-
# Update local cache
|
69 |
-
with open('model_suggestions.json', 'w') as f:
|
70 |
-
json.dump(suggestions, f, indent=2)
|
71 |
-
|
72 |
-
return True
|
73 |
-
except Exception as e:
|
74 |
-
print(f"Error saving suggestions: {e}")
|
75 |
-
return False
|
76 |
-
|
77 |
-
def add_suggestion(model_url: str) -> str:
|
78 |
-
"""Add or update a model suggestion with validation."""
|
79 |
-
# Validate model URL format
|
80 |
-
if not validate_model_url(model_url):
|
81 |
-
return ("❌ Invalid model URL format. Please use either:\n"
|
82 |
-
"- Ollama format: model-name:Q4_K_M\n"
|
83 |
-
"- HuggingFace format: hf.co/username/model-name-GGUF:Q4_K_M")
|
84 |
-
|
85 |
-
# Check if model is already approved
|
86 |
-
if model_url in dict(config.get_approved_models()):
|
87 |
-
return "ℹ️ This model is already in the arena!"
|
88 |
-
|
89 |
-
suggestions = load_suggestions()
|
90 |
-
current_time = datetime.now().isoformat()
|
91 |
-
|
92 |
-
if model_url in suggestions["suggestions"]:
|
93 |
-
suggestions["suggestions"][model_url].update({
|
94 |
-
"count": suggestions["suggestions"][model_url]["count"] + 1,
|
95 |
-
"last_suggested": current_time
|
96 |
-
})
|
97 |
-
message = (f"✨ Model suggestion updated! "
|
98 |
-
f"This model has been suggested {suggestions['suggestions'][model_url]['count']} times.")
|
99 |
-
else:
|
100 |
-
suggestions["suggestions"][model_url] = {
|
101 |
-
"count": 1,
|
102 |
-
"first_suggested": current_time,
|
103 |
-
"last_suggested": current_time
|
104 |
-
}
|
105 |
-
message = "✅ New model suggestion recorded successfully!"
|
106 |
-
|
107 |
-
if save_suggestions(suggestions):
|
108 |
-
return message
|
109 |
-
return "❌ Error saving suggestion. Please try again later."
|
110 |
-
|
111 |
-
def get_suggestions_html() -> str:
|
112 |
-
"""Generate HTML table of model suggestions with improved styling."""
|
113 |
-
suggestions = load_suggestions()
|
114 |
-
|
115 |
-
# Sort suggestions by count (descending) and last suggested date
|
116 |
-
sorted_suggestions = sorted(
|
117 |
-
suggestions["suggestions"].items(),
|
118 |
-
key=lambda x: (x[1]["count"], x[1]["last_suggested"]),
|
119 |
-
reverse=True
|
120 |
-
)
|
121 |
-
|
122 |
-
stats_header = f"""
|
123 |
-
<div class="stats-header">
|
124 |
-
Total Suggestions: {suggestions.get("total_suggestions", 0)} | Last Updated: {suggestions.get("last_updated", "Never").split("T")[0]}
|
125 |
-
</div>
|
126 |
-
"""
|
127 |
-
|
128 |
-
html = f"""
|
129 |
-
<style>
|
130 |
-
.suggestions-table {{
|
131 |
-
width: 100%;
|
132 |
-
border-collapse: collapse;
|
133 |
-
font-family: Arial, sans-serif;
|
134 |
-
margin-top: 20px;
|
135 |
-
}}
|
136 |
-
.suggestions-table th, .suggestions-table td {{
|
137 |
-
border: 1px solid #ddd;
|
138 |
-
padding: 12px;
|
139 |
-
text-align: left;
|
140 |
-
}}
|
141 |
-
.suggestions-table th {{
|
142 |
-
background-color: rgba(255, 255, 255, 0.1);
|
143 |
-
font-weight: bold;
|
144 |
-
}}
|
145 |
-
.rank-column {{
|
146 |
-
width: 60px;
|
147 |
-
text-align: center;
|
148 |
-
}}
|
149 |
-
.count-badge {{
|
150 |
-
background-color: rgba(34, 87, 122, 0.7);
|
151 |
-
color: white;
|
152 |
-
padding: 4px 8px;
|
153 |
-
border-radius: 12px;
|
154 |
-
font-size: 0.9em;
|
155 |
-
}}
|
156 |
-
.stats-header {{
|
157 |
-
font-size: 0.9em;
|
158 |
-
color: #888;
|
159 |
-
margin-bottom: 10px;
|
160 |
-
}}
|
161 |
-
</style>
|
162 |
-
{stats_header}
|
163 |
-
<table class='suggestions-table'>
|
164 |
-
<tr>
|
165 |
-
<th class='rank-column'>Rank</th>
|
166 |
-
<th>Model URL</th>
|
167 |
-
<th>Suggestions</th>
|
168 |
-
<th>First Suggested</th>
|
169 |
-
<th>Last Suggested</th>
|
170 |
-
</tr>
|
171 |
-
"""
|
172 |
-
|
173 |
-
for index, (model_url, data) in enumerate(sorted_suggestions, start=1):
|
174 |
-
rank_display = {1: "🥇", 2: "🥈", 3: "🥉"}.get(index, f"{index}")
|
175 |
-
|
176 |
-
html += f"""
|
177 |
-
<tr>
|
178 |
-
<td class='rank-column'>{rank_display}</td>
|
179 |
-
<td>{model_url}</td>
|
180 |
-
<td><span class="count-badge">{data['count']}</span></td>
|
181 |
-
<td>{data['first_suggested'].split('T')[0]}</td>
|
182 |
-
<td>{data['last_suggested'].split('T')[0]}</td>
|
183 |
-
</tr>
|
184 |
-
"""
|
185 |
-
|
186 |
-
html += "</table>"
|
187 |
-
return html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|