Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from llama_cpp import Llama
|
| 3 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
model_path = hf_hub_download(
|
| 8 |
-
repo_id="
|
| 9 |
-
filename="gemma-
|
| 10 |
)
|
|
|
|
| 11 |
|
| 12 |
-
# Load model
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
def chat(prompt):
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
iface = gr.Interface(fn=chat, inputs="text", outputs="text")
|
| 23 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
|
| 5 |
+
# Download the model from Hugging Face Hub (only first time, then cached)
|
| 6 |
+
print("Downloading model...")
|
| 7 |
model_path = hf_hub_download(
|
| 8 |
+
repo_id="TheBloke/gemma-7b-it-GGUF",
|
| 9 |
+
filename="gemma-7b-it.Q4_K_M.gguf"
|
| 10 |
)
|
| 11 |
+
print("Model downloaded to:", model_path)
|
| 12 |
|
| 13 |
+
# Load the model with llama-cpp-python
|
| 14 |
+
print("Loading model...")
|
| 15 |
+
llm = Llama(
|
| 16 |
+
model_path=model_path,
|
| 17 |
+
n_ctx=2048 # You can adjust this as per your memory limits
|
| 18 |
+
)
|
| 19 |
+
print("Model loaded.")
|
| 20 |
|
| 21 |
+
# Define a function to interact with the model
|
| 22 |
def chat(prompt):
|
| 23 |
+
print(f"User input: {prompt}")
|
| 24 |
+
output = llm(
|
| 25 |
+
prompt,
|
| 26 |
+
max_tokens=512,
|
| 27 |
+
stop=["</s>", "User:", "Assistant:"]
|
| 28 |
+
)
|
| 29 |
+
reply = output['choices'][0]['text'].strip()
|
| 30 |
+
print(f"Model reply: {reply}")
|
| 31 |
+
return reply
|
| 32 |
+
|
| 33 |
+
# Create a simple Gradio UI
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=chat,
|
| 36 |
+
inputs="text",
|
| 37 |
+
outputs="text",
|
| 38 |
+
title="Gemma 7B IT Chatbot",
|
| 39 |
+
description="Running a GGUF model (Gemma 7B IT) on Hugging Face Space using llama-cpp-python"
|
| 40 |
+
)
|
| 41 |
|
| 42 |
+
# Launch the app
|
|
|
|
| 43 |
iface.launch()
|