Spaces:
Sleeping
Sleeping
alesb2010
commited on
Commit
·
5cf7c39
1
Parent(s):
fb897fa
Update space
Browse files- app.py +44 -22
- requirements.txt +2 -1
app.py
CHANGED
@@ -7,9 +7,19 @@ import os # Useful for environment variables if needed
|
|
7 |
# Replace "your-model-id" with the actual ID of the model on Hugging Face Hub
|
8 |
# Using pipeline is often the easiest way to start for common tasks
|
9 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Example: Sentiment Analysis model
|
11 |
# model = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
12 |
-
model = AutoModel.from_pretrained("mradermacher/DeepSeek-R1-Distill-Qwen-7B-Multilingual-i1-GGUF")
|
13 |
# Or load specific model/tokenizer if pipeline isn't suitable:
|
14 |
# from transformers import AutoModel, AutoTokenizer
|
15 |
# tokenizer = AutoTokenizer.from_pretrained("your-model-id")
|
@@ -23,39 +33,51 @@ except Exception as e:
|
|
23 |
# 2. Define the function that uses the model
|
24 |
# This function takes the input from the Gradio interface
|
25 |
# and returns the output that Gradio will display.
|
26 |
-
def
|
27 |
-
if
|
28 |
-
return "Model
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
39 |
|
40 |
|
41 |
# 3. Define the Gradio Interface
|
42 |
-
|
43 |
-
if model is not None: # Only create the interface if the model loaded successfully
|
44 |
interface = gr.Interface(
|
45 |
-
fn=
|
46 |
-
inputs=gr.Textbox(label="Enter
|
47 |
-
outputs=gr.
|
48 |
-
title="
|
49 |
-
description="
|
50 |
)
|
51 |
else:
|
52 |
-
#
|
53 |
interface = gr.Interface(
|
54 |
-
fn=lambda x: "Application failed to load model.
|
55 |
inputs=gr.Textbox(label="Status"),
|
56 |
outputs=gr.Textbox(),
|
57 |
title="Application Error",
|
58 |
-
description="Failed to load the model. Check the logs for details."
|
59 |
)
|
60 |
|
61 |
|
|
|
7 |
# Replace "your-model-id" with the actual ID of the model on Hugging Face Hub
|
8 |
# Using pipeline is often the easiest way to start for common tasks
|
9 |
try:
|
10 |
+
from llama_cpp import Llama
|
11 |
+
print("llama_cpp imported successfully")
|
12 |
+
except ImportError:
|
13 |
+
print("Error: llama-cpp-python not installed. Please check requirements.txt and logs.")
|
14 |
+
Llama = None # Set to None if import fails
|
15 |
+
llm = None
|
16 |
+
if Llama is not None:
|
17 |
+
try:
|
18 |
+
model_repo_id = "mradermacher/DeepSeek-R1-Distill-Qwen-7B-Multilingual-i1-GGUF"
|
19 |
+
model_file_name = "deepseek-r1-distill-qwen-7b-multilingual-i1.Q4_K_M.gguf" # <<== VERIFY THIS FILENAME ON HF HUB
|
20 |
# Example: Sentiment Analysis model
|
21 |
# model = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
22 |
+
# model = AutoModel.from_pretrained("mradermacher/DeepSeek-R1-Distill-Qwen-7B-Multilingual-i1-GGUF")
|
23 |
# Or load specific model/tokenizer if pipeline isn't suitable:
|
24 |
# from transformers import AutoModel, AutoTokenizer
|
25 |
# tokenizer = AutoTokenizer.from_pretrained("your-model-id")
|
|
|
33 |
# 2. Define the function that uses the model
|
34 |
# This function takes the input from the Gradio interface
|
35 |
# and returns the output that Gradio will display.
|
36 |
+
def generate_text(prompt):
|
37 |
+
if llm is None:
|
38 |
+
return "Model failed to load. Please check App Space logs."
|
39 |
|
40 |
+
try:
|
41 |
+
print(f"Generating completion for prompt: {prompt[:100]}...") # Log start of generation
|
42 |
+
# Use the model to generate text
|
43 |
+
# Adjust max_tokens, stop sequence, etc. based on your needs and the model
|
44 |
+
output = llm(
|
45 |
+
prompt,
|
46 |
+
max_tokens=512, # Max tokens to generate
|
47 |
+
stop=["Qwen:", "\n\n"], # Stop sequence examples (adjust as needed)
|
48 |
+
echo=False, # Don't include prompt in output
|
49 |
+
temperature=0.7, # Creativity level
|
50 |
+
top_p=0.9, # Nucleus sampling
|
51 |
+
)
|
52 |
+
print("Generation complete.")
|
53 |
|
54 |
+
# Extract the generated text
|
55 |
+
generated_text = output["choices"][0]["text"]
|
56 |
+
|
57 |
+
return generated_text
|
58 |
+
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Error during text generation: {e}")
|
61 |
+
return f"An error occurred during generation: {e}"
|
62 |
|
63 |
|
64 |
# 3. Define the Gradio Interface
|
65 |
+
if llm is not None: # Only create the interface if the model loaded successfully
|
|
|
66 |
interface = gr.Interface(
|
67 |
+
fn=generate_text, # Your new generation function
|
68 |
+
inputs=gr.Textbox(label="Enter your prompt", lines=5), # Text input
|
69 |
+
outputs=gr.Textbox(label="Generated Text", lines=10), # Text output
|
70 |
+
title="DeepSeek-R1-Distill-Qwen-7B GGUF Demo",
|
71 |
+
description="Interact with the DeepSeek-R1-Distill-Qwen-7B Multilingual model in GGUF format."
|
72 |
)
|
73 |
else:
|
74 |
+
# Interface to show error if model loading failed
|
75 |
interface = gr.Interface(
|
76 |
+
fn=lambda x: "Application failed to load model. See logs for details.",
|
77 |
inputs=gr.Textbox(label="Status"),
|
78 |
outputs=gr.Textbox(),
|
79 |
title="Application Error",
|
80 |
+
description="Failed to load the GGUF model. Check the logs for details on model loading errors."
|
81 |
)
|
82 |
|
83 |
|
requirements.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
huggingface_hub==0.25.2
|
2 |
gradio
|
3 |
transformers
|
4 |
-
torch
|
|
|
|
1 |
huggingface_hub==0.25.2
|
2 |
gradio
|
3 |
transformers
|
4 |
+
torch
|
5 |
+
llama-cpp-python
|