Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,61 @@
|
|
1 |
-
from llama_cpp import Llama
|
2 |
-
from huggingface_hub import hf_hub_download
|
3 |
import gradio as gr
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
""
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
return
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
""
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
chatbot = gr.Chatbot()
|
65 |
-
with gr.Row():
|
66 |
-
with gr.Column(scale=4):
|
67 |
-
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
|
68 |
-
with gr.Column(scale=1):
|
69 |
-
submit_btn = gr.Button("Send")
|
70 |
-
with gr.Row():
|
71 |
-
clear_btn = gr.Button("Clear Chat")
|
72 |
-
|
73 |
-
submit_btn.click(chat_with_model, inputs=user_input, outputs=chatbot)
|
74 |
-
clear_btn.click(reset_history, inputs=[], outputs=[user_input, chatbot])
|
75 |
-
|
76 |
-
# Launch the interface
|
77 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from llama_cpp import Llama
|
3 |
+
import os
|
4 |
+
import requests
|
5 |
+
import tempfile
|
6 |
+
|
7 |
+
MODEL_PATH = "model.gguf" # Default model name
|
8 |
+
MODEL_URL = None # Optional URL to download the model from
|
9 |
+
|
10 |
+
def download_model(url, save_path):
|
11 |
+
"""Downloads a file from a URL."""
|
12 |
+
response = requests.get(url, stream=True)
|
13 |
+
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
|
14 |
+
|
15 |
+
with open(save_path, "wb") as file:
|
16 |
+
for chunk in response.iter_content(chunk_size=8192):
|
17 |
+
file.write(chunk)
|
18 |
+
|
19 |
+
def load_model(model_path):
|
20 |
+
"""Loads a GGUF model using llama-cpp-python."""
|
21 |
+
try:
|
22 |
+
llm = Llama(model_path)
|
23 |
+
return llm
|
24 |
+
except Exception as e:
|
25 |
+
return f"Error loading model: {e}"
|
26 |
+
|
27 |
+
def generate_response(prompt, model):
|
28 |
+
"""Generates a response using the loaded model."""
|
29 |
+
if isinstance(model, str): # if there was an error loading the model
|
30 |
+
return model
|
31 |
+
try:
|
32 |
+
output = model(prompt, max_tokens=256) # Adjust max_tokens as needed
|
33 |
+
return output["choices"][0]["text"]
|
34 |
+
except Exception as e:
|
35 |
+
return f"Error generating response: {e}"
|
36 |
+
|
37 |
+
# Download model if URL is provided and model doesn't exist
|
38 |
+
if MODEL_URL and not os.path.exists(MODEL_PATH):
|
39 |
+
print(f"Downloading model from {MODEL_URL}...")
|
40 |
+
try:
|
41 |
+
download_model(MODEL_URL, MODEL_PATH)
|
42 |
+
print("Model downloaded successfully.")
|
43 |
+
except Exception as e:
|
44 |
+
print(f"Error downloading model: {e}")
|
45 |
+
|
46 |
+
# Load the model
|
47 |
+
llm = load_model(MODEL_PATH)
|
48 |
+
|
49 |
+
def inference(prompt):
|
50 |
+
return generate_response(prompt, llm)
|
51 |
+
|
52 |
+
# Gradio Interface
|
53 |
+
iface = gr.Interface(
|
54 |
+
fn=inference,
|
55 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter your prompt here..."),
|
56 |
+
outputs=gr.Textbox(lines=10),
|
57 |
+
title="llama.cpp CPU Inference",
|
58 |
+
description="Generate text using a GGUF model with llama.cpp on CPU.",
|
59 |
+
)
|
60 |
+
|
61 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|