Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,21 @@
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
|
4 |
-
# Function to load a model using Hugging Face Spaces
|
5 |
-
def
|
6 |
-
print(f"Attempting to load {model_name}
|
7 |
try:
|
8 |
-
# Use subprocess to run hf.space_info and get GPU setting
|
9 |
-
result = subprocess.run(
|
10 |
-
["python", "-c", f"from huggingface_hub import space_info; print(space_info('{model_name}').hardware)"],
|
11 |
-
capture_output=True,
|
12 |
-
text=True,
|
13 |
-
check=True
|
14 |
-
)
|
15 |
-
hardware = result.stdout.strip()
|
16 |
-
print(f"Hardware for {model_name}: {hardware}")
|
17 |
-
|
18 |
demo = gr.load(name=model_name, src="spaces")
|
19 |
-
|
20 |
-
# Return the loaded model demo
|
21 |
print(f"Successfully loaded {model_name}")
|
22 |
return demo
|
23 |
-
|
24 |
except Exception as e:
|
25 |
print(f"Error loading model {model_name}: {e}")
|
26 |
return None
|
27 |
|
28 |
-
# Load the models
|
29 |
-
deepseek_r1_distill =
|
30 |
-
deepseek_r1 =
|
31 |
-
deepseek_r1_zero =
|
32 |
|
33 |
# --- Chatbot function ---
|
34 |
def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
|
@@ -46,16 +33,22 @@ def chatbot(input_text, history, model_choice, system_message, max_new_tokens, t
|
|
46 |
default_response = "Model not selected or could not be loaded."
|
47 |
history.append((input_text, default_response))
|
48 |
return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
|
49 |
-
|
50 |
-
#
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# Check if model_output is iterable and has expected number of elements
|
54 |
if not isinstance(model_output, (list, tuple)) or len(model_output) < 2:
|
55 |
error_message = "Model output does not have the expected format."
|
56 |
history.append((input_text, error_message))
|
57 |
return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
|
58 |
-
|
59 |
response = model_output[-1][1] if model_output[-1][1] else "Model did not return a response."
|
60 |
history.append((input_text, response))
|
61 |
return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
|
|
|
1 |
import gradio as gr
|
2 |
import subprocess
|
3 |
|
4 |
+
# Function to load a model using Hugging Face Spaces
|
5 |
+
def load_model_from_space(model_name):
|
6 |
+
print(f"Attempting to load {model_name}...")
|
7 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
demo = gr.load(name=model_name, src="spaces")
|
|
|
|
|
9 |
print(f"Successfully loaded {model_name}")
|
10 |
return demo
|
|
|
11 |
except Exception as e:
|
12 |
print(f"Error loading model {model_name}: {e}")
|
13 |
return None
|
14 |
|
15 |
+
# Load the models
|
16 |
+
deepseek_r1_distill = load_model_from_space("deepseek-ai/DeepSeek-R1-Distill-Qwen-32B")
|
17 |
+
deepseek_r1 = load_model_from_space("deepseek-ai/DeepSeek-R1")
|
18 |
+
deepseek_r1_zero = load_model_from_space("deepseek-ai/DeepSeek-R1-Zero")
|
19 |
|
20 |
# --- Chatbot function ---
|
21 |
def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
|
|
|
33 |
default_response = "Model not selected or could not be loaded."
|
34 |
history.append((input_text, default_response))
|
35 |
return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
|
36 |
+
|
37 |
+
# Call the model's 'predict' function.
|
38 |
+
try:
|
39 |
+
model_output = model_demo(input_text, history, max_new_tokens, temperature, top_p, system_message, fn_index=0)
|
40 |
+
except Exception as e:
|
41 |
+
print(f"An error occurred: {e}")
|
42 |
+
model_output= "An error occurred please check the model and try again."
|
43 |
+
history.append((input_text, model_output))
|
44 |
+
return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
|
45 |
|
46 |
# Check if model_output is iterable and has expected number of elements
|
47 |
if not isinstance(model_output, (list, tuple)) or len(model_output) < 2:
|
48 |
error_message = "Model output does not have the expected format."
|
49 |
history.append((input_text, error_message))
|
50 |
return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
|
51 |
+
|
52 |
response = model_output[-1][1] if model_output[-1][1] else "Model did not return a response."
|
53 |
history.append((input_text, response))
|
54 |
return history, history, "", model_choice, system_message, max_new_tokens, temperature, top_p
|