Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
# =========================
|
| 8 |
+
# 1. DOWNLOAD MODEL
|
| 9 |
+
# =========================
|
| 10 |
+
MODEL_PATH = hf_hub_download(
|
| 11 |
+
repo_id="unsloth/gemma-4-E4B-it-GGUF",
|
| 12 |
+
filename="gemma-4-E4B-it-IQ4_XS.gguf"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
print("Model downloaded:", MODEL_PATH)
|
| 16 |
+
|
| 17 |
+
# =========================
|
| 18 |
+
# 2. START llama.cpp SERVER
|
| 19 |
+
# =========================
|
| 20 |
+
server = subprocess.Popen([
|
| 21 |
+
"/llama.cpp/build/bin/llama-server",
|
| 22 |
+
"-m", MODEL_PATH,
|
| 23 |
+
"-c", "2048",
|
| 24 |
+
"-t", "2",
|
| 25 |
+
"-ngl", "0",
|
| 26 |
+
"--host", "127.0.0.1",
|
| 27 |
+
"--port", "8080"
|
| 28 |
+
])
|
| 29 |
+
|
| 30 |
+
time.sleep(6)
|
| 31 |
+
|
| 32 |
+
# =========================
|
| 33 |
+
# 3. GRADIO CHAT FUNCTION
|
| 34 |
+
# =========================
|
| 35 |
+
def chat(message, history):
|
| 36 |
+
payload = {
|
| 37 |
+
"messages": [
|
| 38 |
+
{"role": "user", "content": message}
|
| 39 |
+
]
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
r = requests.post(
|
| 43 |
+
"http://127.0.0.1:8080/v1/chat/completions",
|
| 44 |
+
json=payload,
|
| 45 |
+
timeout=300
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
return r.json()["choices"][0]["message"]["content"]
|
| 49 |
+
|
| 50 |
+
# =========================
|
| 51 |
+
# 4. GRADIO UI
|
| 52 |
+
# =========================
|
| 53 |
+
demo = gr.ChatInterface(chat)
|
| 54 |
+
|
| 55 |
+
demo.launch(
|
| 56 |
+
server_name="0.0.0.0",
|
| 57 |
+
server_port=7860
|
| 58 |
+
)
|