Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,39 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
import subprocess
|
|
|
|
| 4 |
from llama_cpp import Llama
|
|
|
|
| 5 |
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Model
|
| 8 |
-
MODEL_URL = "https://huggingface.co/TheBloke/mistral-7b.Q4_K_M.gguf"
|
| 9 |
MODEL_PATH = "./models/mistral-7b.Q4_K_M.gguf"
|
| 10 |
|
| 11 |
-
#
|
| 12 |
os.makedirs("./models", exist_ok=True)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
if not os.path.exists(MODEL_PATH):
|
| 16 |
-
print("Downloading Mistral-7B Q4 GGUF model...")
|
| 17 |
-
subprocess.run([
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Load
|
| 20 |
-
print("Loading model...")
|
| 21 |
model = Llama(model_path=MODEL_PATH, n_ctx=4096, n_threads=8)
|
|
|
|
| 22 |
|
| 23 |
-
# Define function
|
| 24 |
-
def
|
| 25 |
-
response = model(prompt, max_tokens=512
|
| 26 |
return response["choices"][0]["text"]
|
| 27 |
|
| 28 |
-
# Gradio UI
|
| 29 |
-
iface = gr.Interface(fn=
|
|
|
|
|
|
|
| 30 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
+
import os
|
| 3 |
from llama_cpp import Llama
|
| 4 |
+
import gradio as gr
|
| 5 |
|
| 6 |
+
# ๐น Get Hugging Face Token from environment variable
|
| 7 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 8 |
|
| 9 |
+
# ๐น Model details
|
| 10 |
+
MODEL_URL = "https://huggingface.co/TheBloke/Mistral-7B-GGUF/resolve/main/mistral-7b.Q4_K_M.gguf"
|
| 11 |
MODEL_PATH = "./models/mistral-7b.Q4_K_M.gguf"
|
| 12 |
|
| 13 |
+
# ๐น Ensure the models directory exists
|
| 14 |
os.makedirs("./models", exist_ok=True)
|
| 15 |
|
| 16 |
+
# ๐น Check if the model exists, else download it
|
| 17 |
if not os.path.exists(MODEL_PATH):
|
| 18 |
+
print("๐ Downloading Mistral-7B Q4 GGUF model...")
|
| 19 |
+
subprocess.run([
|
| 20 |
+
"wget", "--header", f"Authorization: Bearer {HF_TOKEN}",
|
| 21 |
+
MODEL_URL, "-O", MODEL_PATH
|
| 22 |
+
], check=True)
|
| 23 |
+
print("โ
Download complete!")
|
| 24 |
|
| 25 |
+
# ๐น Load the model
|
| 26 |
+
print("๐ฅ Loading the model...")
|
| 27 |
model = Llama(model_path=MODEL_PATH, n_ctx=4096, n_threads=8)
|
| 28 |
+
print("โ
Model loaded successfully!")
|
| 29 |
|
| 30 |
+
# ๐น Define a function to interact with the model
|
| 31 |
+
def chat_with_mistral(prompt):
|
| 32 |
+
response = model(prompt, max_tokens=512)
|
| 33 |
return response["choices"][0]["text"]
|
| 34 |
|
| 35 |
+
# ๐น Create a Gradio UI
|
| 36 |
+
iface = gr.Interface(fn=chat_with_mistral, inputs="text", outputs="text", title="Mistral-7B Chatbot")
|
| 37 |
+
|
| 38 |
+
# ๐น Launch the app
|
| 39 |
iface.launch()
|