Spaces:
Running
Running
space update
Browse files
app.py
CHANGED
@@ -114,8 +114,54 @@ except Exception as e:
|
|
114 |
print(f"β Failed to initialize model: {e}")
|
115 |
model, tokenizer, model_status = None, None, f"β Error: {e}"
|
116 |
|
117 |
-
def
|
118 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
global model, tokenizer, model_status
|
120 |
|
121 |
if uploaded_file is None:
|
@@ -188,64 +234,58 @@ def respond(
|
|
188 |
except Exception as e:
|
189 |
yield f"Sorry, I encountered an error: {str(e)}"
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
-
#
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
gr.Markdown("### π Model Upload (Optional)")
|
205 |
-
model_file = gr.File(
|
206 |
-
label="Upload your own model.pth file",
|
207 |
-
file_types=[".pth", ".pt"]
|
208 |
-
)
|
209 |
-
upload_btn = gr.Button("Load Model", variant="primary")
|
210 |
-
model_status_display = gr.Textbox(
|
211 |
-
label="Model Status",
|
212 |
-
value=model_status,
|
213 |
-
interactive=False
|
214 |
-
)
|
215 |
-
|
216 |
-
with gr.Column(scale=1):
|
217 |
-
# Settings
|
218 |
-
with gr.Group():
|
219 |
-
gr.Markdown("### βοΈ Generation Settings")
|
220 |
-
system_msg = gr.Textbox(
|
221 |
-
value="You are Usta, a geographical knowledge assistant trained from scratch.",
|
222 |
-
label="System message"
|
223 |
-
)
|
224 |
-
max_tokens = gr.Slider(minimum=1, maximum=30, value=20, step=1, label="Max new tokens")
|
225 |
-
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Temperature")
|
226 |
-
top_p = gr.Slider(
|
227 |
-
minimum=0.1,
|
228 |
-
maximum=1.0,
|
229 |
-
value=0.95,
|
230 |
-
step=0.05,
|
231 |
-
label="Top-p (nucleus sampling)"
|
232 |
-
)
|
233 |
|
234 |
-
#
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
title=None, # We already have title above
|
240 |
-
description=None # We already have description above
|
241 |
)
|
242 |
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
outputs=[model_status_display]
|
248 |
)
|
249 |
|
|
|
|
|
|
|
250 |
if __name__ == "__main__":
|
251 |
demo.launch()
|
|
|
114 |
print(f"β Failed to initialize model: {e}")
|
115 |
model, tokenizer, model_status = None, None, f"β Error: {e}"
|
116 |
|
117 |
+
def load_model_from_url(url):
|
118 |
+
"""Load model from a URL"""
|
119 |
+
global model, tokenizer, model_status
|
120 |
+
|
121 |
+
if not url.strip():
|
122 |
+
return "β Please provide a URL"
|
123 |
+
|
124 |
+
try:
|
125 |
+
print(f"π₯ Downloading model from URL: {url}")
|
126 |
+
import requests
|
127 |
+
|
128 |
+
headers = {
|
129 |
+
'Accept': 'application/octet-stream',
|
130 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
|
131 |
+
}
|
132 |
+
|
133 |
+
response = requests.get(url, headers=headers)
|
134 |
+
response.raise_for_status()
|
135 |
+
|
136 |
+
# Check if we got a proper binary file
|
137 |
+
if response.content[:4] != b'PK\x03\x04' and b'<html' in response.content[:100].lower():
|
138 |
+
return "β Downloaded HTML instead of binary file - check URL"
|
139 |
+
|
140 |
+
# Save temporary file
|
141 |
+
temp_path = "temp_model.pth"
|
142 |
+
with open(temp_path, "wb") as f:
|
143 |
+
f.write(response.content)
|
144 |
+
|
145 |
+
# Load the model
|
146 |
+
new_model, new_tokenizer, status = load_model(temp_path)
|
147 |
+
|
148 |
+
# Update global variables
|
149 |
+
model = new_model
|
150 |
+
tokenizer = new_tokenizer
|
151 |
+
model_status = status
|
152 |
+
|
153 |
+
# Clean up temp file
|
154 |
+
if os.path.exists(temp_path):
|
155 |
+
os.remove(temp_path)
|
156 |
+
|
157 |
+
return status
|
158 |
+
except Exception as e:
|
159 |
+
error_msg = f"β Failed to load model from URL: {e}"
|
160 |
+
model_status = error_msg
|
161 |
+
return error_msg
|
162 |
+
|
163 |
+
def load_model_from_file(uploaded_file):
|
164 |
+
"""Load model from uploaded file"""
|
165 |
global model, tokenizer, model_status
|
166 |
|
167 |
if uploaded_file is None:
|
|
|
234 |
except Exception as e:
|
235 |
yield f"Sorry, I encountered an error: {str(e)}"
|
236 |
|
237 |
+
# Create the simple ChatInterface with additional inputs for model loading
|
238 |
+
demo = gr.ChatInterface(
|
239 |
+
respond,
|
240 |
+
additional_inputs=[
|
241 |
+
gr.Textbox(
|
242 |
+
value="You are Usta, a geographical knowledge assistant trained from scratch.",
|
243 |
+
label="System message"
|
244 |
+
),
|
245 |
+
gr.Slider(minimum=1, maximum=30, value=20, step=1, label="Max new tokens"),
|
246 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Temperature"),
|
247 |
+
gr.Slider(
|
248 |
+
minimum=0.1,
|
249 |
+
maximum=1.0,
|
250 |
+
value=0.95,
|
251 |
+
step=0.05,
|
252 |
+
label="Top-p (nucleus sampling)"
|
253 |
+
),
|
254 |
+
gr.File(label="Upload Model File (.pth)", file_types=[".pth", ".pt"]),
|
255 |
+
gr.Textbox(label="Or Model URL", placeholder="https://github.com/user/repo/raw/main/model.pth"),
|
256 |
+
gr.Button("Load from File", variant="secondary"),
|
257 |
+
gr.Button("Load from URL", variant="secondary"),
|
258 |
+
gr.Textbox(label="Model Status", value=model_status, interactive=False)
|
259 |
+
],
|
260 |
+
title="π€ Usta Model Chat",
|
261 |
+
description="Chat with a custom transformer language model built from scratch! Upload your own model file or provide a URL to load a different model."
|
262 |
+
)
|
263 |
|
264 |
+
# Add event handlers after creating the interface
|
265 |
+
def setup_events():
|
266 |
+
# Get the additional inputs
|
267 |
+
inputs = demo.additional_inputs
|
268 |
+
model_file = inputs[4] # File upload
|
269 |
+
model_url = inputs[5] # URL input
|
270 |
+
load_file_btn = inputs[6] # Load from file button
|
271 |
+
load_url_btn = inputs[7] # Load from URL button
|
272 |
+
status_display = inputs[8] # Status display
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
273 |
|
274 |
+
# Set up event handlers
|
275 |
+
load_file_btn.click(
|
276 |
+
load_model_from_file,
|
277 |
+
inputs=[model_file],
|
278 |
+
outputs=[status_display]
|
|
|
|
|
279 |
)
|
280 |
|
281 |
+
load_url_btn.click(
|
282 |
+
load_model_from_url,
|
283 |
+
inputs=[model_url],
|
284 |
+
outputs=[status_display]
|
|
|
285 |
)
|
286 |
|
287 |
+
# Set up events after interface creation
|
288 |
+
demo.load(setup_events)
|
289 |
+
|
290 |
if __name__ == "__main__":
|
291 |
demo.launch()
|