Update app.py
Browse files
app.py
CHANGED
|
@@ -5,76 +5,87 @@ import requests
|
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
-
MODEL_REPO = "bartowski/
|
| 9 |
-
MODEL_FILE = "
|
| 10 |
|
| 11 |
LLAMA_DIR = "llama.cpp"
|
| 12 |
SERVER_PORT = "8000"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
repo_id=MODEL_REPO,
|
| 17 |
filename=MODEL_FILE
|
| 18 |
)
|
| 19 |
-
|
| 20 |
|
| 21 |
def setup_llama():
|
| 22 |
if not os.path.exists(LLAMA_DIR):
|
| 23 |
subprocess.run(
|
| 24 |
-
["git","clone","https://github.com/ggerganov/llama.cpp"],
|
| 25 |
check=True
|
| 26 |
)
|
| 27 |
|
| 28 |
subprocess.run(
|
| 29 |
-
["make","-j"],
|
| 30 |
cwd=LLAMA_DIR,
|
| 31 |
check=True
|
| 32 |
)
|
| 33 |
|
|
|
|
| 34 |
def start_server(model_path):
|
| 35 |
-
subprocess.Popen(
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
def wait_for_server():
|
| 44 |
url = f"http://localhost:{SERVER_PORT}/health"
|
| 45 |
-
for _ in range(
|
| 46 |
try:
|
| 47 |
requests.get(url, timeout=1)
|
| 48 |
return
|
| 49 |
except:
|
| 50 |
time.sleep(2)
|
| 51 |
|
|
|
|
| 52 |
def chat(prompt):
|
| 53 |
r = requests.post(
|
| 54 |
f"http://localhost:{SERVER_PORT}/v1/chat/completions",
|
| 55 |
json={
|
| 56 |
-
"model": "
|
| 57 |
"messages": [
|
| 58 |
-
{"role":"user","content": prompt}
|
| 59 |
]
|
| 60 |
},
|
| 61 |
-
timeout=
|
| 62 |
)
|
| 63 |
|
| 64 |
data = r.json()
|
| 65 |
return data["choices"][0]["message"]["content"]
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
setup_llama()
|
|
|
|
| 69 |
start_server(model_path)
|
|
|
|
| 70 |
wait_for_server()
|
| 71 |
|
|
|
|
| 72 |
ui = gr.Interface(
|
| 73 |
fn=chat,
|
| 74 |
-
inputs=gr.Textbox(lines=
|
| 75 |
outputs="text",
|
| 76 |
-
title="Operon Dev LLM",
|
| 77 |
-
description="Local llama.cpp server running on Hugging Face Space"
|
| 78 |
)
|
| 79 |
|
| 80 |
ui.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
| 8 |
+
MODEL_REPO = "bartowski/Qwen_Qwen3.5-9B-GGUF"
|
| 9 |
+
MODEL_FILE = "Qwen3.5-9B-Q4_K_M.gguf"
|
| 10 |
|
| 11 |
LLAMA_DIR = "llama.cpp"
|
| 12 |
SERVER_PORT = "8000"
|
| 13 |
|
| 14 |
+
|
| 15 |
+
def download_model():
|
| 16 |
+
return hf_hub_download(
|
| 17 |
repo_id=MODEL_REPO,
|
| 18 |
filename=MODEL_FILE
|
| 19 |
)
|
| 20 |
+
|
| 21 |
|
| 22 |
def setup_llama():
|
| 23 |
if not os.path.exists(LLAMA_DIR):
|
| 24 |
subprocess.run(
|
| 25 |
+
["git", "clone", "https://github.com/ggerganov/llama.cpp"],
|
| 26 |
check=True
|
| 27 |
)
|
| 28 |
|
| 29 |
subprocess.run(
|
| 30 |
+
["make", "-j"],
|
| 31 |
cwd=LLAMA_DIR,
|
| 32 |
check=True
|
| 33 |
)
|
| 34 |
|
| 35 |
+
|
| 36 |
def start_server(model_path):
|
| 37 |
+
subprocess.Popen(
|
| 38 |
+
[
|
| 39 |
+
"./server",
|
| 40 |
+
"-m", model_path,
|
| 41 |
+
"--port", SERVER_PORT,
|
| 42 |
+
"--ctx-size", "4096",
|
| 43 |
+
"--api"
|
| 44 |
+
],
|
| 45 |
+
cwd=LLAMA_DIR
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
|
| 49 |
def wait_for_server():
|
| 50 |
url = f"http://localhost:{SERVER_PORT}/health"
|
| 51 |
+
for _ in range(60):
|
| 52 |
try:
|
| 53 |
requests.get(url, timeout=1)
|
| 54 |
return
|
| 55 |
except:
|
| 56 |
time.sleep(2)
|
| 57 |
|
| 58 |
+
|
| 59 |
def chat(prompt):
|
| 60 |
r = requests.post(
|
| 61 |
f"http://localhost:{SERVER_PORT}/v1/chat/completions",
|
| 62 |
json={
|
| 63 |
+
"model": "qwen3.5-9b",
|
| 64 |
"messages": [
|
| 65 |
+
{"role": "user", "content": prompt}
|
| 66 |
]
|
| 67 |
},
|
| 68 |
+
timeout=300
|
| 69 |
)
|
| 70 |
|
| 71 |
data = r.json()
|
| 72 |
return data["choices"][0]["message"]["content"]
|
| 73 |
|
| 74 |
+
|
| 75 |
+
model_path = download_model()
|
| 76 |
+
|
| 77 |
setup_llama()
|
| 78 |
+
|
| 79 |
start_server(model_path)
|
| 80 |
+
|
| 81 |
wait_for_server()
|
| 82 |
|
| 83 |
+
|
| 84 |
ui = gr.Interface(
|
| 85 |
fn=chat,
|
| 86 |
+
inputs=gr.Textbox(lines=5, placeholder="Ask Operon something..."),
|
| 87 |
outputs="text",
|
| 88 |
+
title="Operon Dev LLM (Qwen3.5-9B)",
|
|
|
|
| 89 |
)
|
| 90 |
|
| 91 |
ui.launch(server_name="0.0.0.0", server_port=7860)
|