alesb2010 commited on
Commit
5cf7c39
·
1 Parent(s): fb897fa

Update space

Browse files
Files changed (2) hide show
  1. app.py +44 -22
  2. 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 process_input_with_model(input_text):
27
- if model is None:
28
- return "Model could not be loaded. Please check logs."
29
 
30
- # Example using a pipeline:
31
- result = model(input_text)
32
- return result[0]['label'] # Adjust based on your model's output format
 
 
 
 
 
 
 
 
 
 
33
 
34
- # Example if you loaded model/tokenizer manually:
35
- # inputs = tokenizer(input_text, return_tensors="pt")
36
- # outputs = model(**inputs)
37
- # Process outputs to get your desired result...
38
- # return processed_result
 
 
 
39
 
40
 
41
  # 3. Define the Gradio Interface
42
- # Set up the input and output components and link the processing function
43
- if model is not None: # Only create the interface if the model loaded successfully
44
  interface = gr.Interface(
45
- fn=process_input_with_model, # Your function
46
- inputs=gr.Textbox(label="Enter text for analysis"), # Input component (adjust type as needed)
47
- outputs=gr.Label(), # Output component (adjust type as needed)
48
- title="My Hugging Face Model Test App",
49
- description="Test out the sentiment analysis model."
50
  )
51
  else:
52
- # Create a simple interface indicating an error if the model failed to load
53
  interface = gr.Interface(
54
- fn=lambda x: "Application failed to load model.", # Simple function
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