Spaces:
Sleeping
Sleeping
Commit ·
a3090ac
1
Parent(s): 7060f14
gradio space
Browse files
apps/gradio-space/README.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# gradio-space
|
| 2 |
+
|
| 3 |
+
Gradio chat UI package for the hackathon Space. Entrypoint: `python -m gradio_space.app`.
|
| 4 |
+
|
| 5 |
+
Space card metadata lives in the [repository root README.md](../../README.md).
|
apps/gradio-space/pyproject.toml
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "gradio-space"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Gradio chat UI for the Build Small Hackathon Space"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
authors = [
|
| 7 |
+
{ name = "MSGhais", email = "msghais135@gmail.com" }
|
| 8 |
+
]
|
| 9 |
+
requires-python = ">=3.12"
|
| 10 |
+
dependencies = [
|
| 11 |
+
"gradio>=5.0.0",
|
| 12 |
+
"inference",
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
[tool.uv.sources]
|
| 16 |
+
inference = { workspace = true }
|
| 17 |
+
|
| 18 |
+
[build-system]
|
| 19 |
+
requires = ["uv_build>=0.8.13,<0.9.0"]
|
| 20 |
+
build-backend = "uv_build"
|
apps/gradio-space/src/gradio_space/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gradio_space.app import build_demo, demo, main
|
| 2 |
+
|
| 3 |
+
__all__ = ["build_demo", "demo", "main"]
|
apps/gradio-space/src/gradio_space/app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from inference.factory import get_backend
|
| 6 |
+
|
| 7 |
+
_backend = get_backend()
|
| 8 |
+
_model_ready = False
|
| 9 |
+
_load_error: str | None = None
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def _ensure_model_loaded() -> str | None:
|
| 13 |
+
global _model_ready, _load_error
|
| 14 |
+
|
| 15 |
+
if _model_ready:
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
if _load_error:
|
| 19 |
+
return _load_error
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
_backend.load()
|
| 23 |
+
_model_ready = True
|
| 24 |
+
return None
|
| 25 |
+
except Exception as exc: # noqa: BLE001 — surface model load failures in the UI
|
| 26 |
+
_load_error = f"Failed to load model: {exc}"
|
| 27 |
+
return _load_error
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def chat(message: str, history: list) -> str:
|
| 31 |
+
load_error = _ensure_model_loaded()
|
| 32 |
+
if load_error:
|
| 33 |
+
return load_error
|
| 34 |
+
|
| 35 |
+
messages: list[dict[str, str]] = []
|
| 36 |
+
for item in history:
|
| 37 |
+
if isinstance(item, dict):
|
| 38 |
+
messages.append({"role": item["role"], "content": item["content"]})
|
| 39 |
+
else:
|
| 40 |
+
user_msg, assistant_msg = item
|
| 41 |
+
messages.append({"role": "user", "content": user_msg})
|
| 42 |
+
if assistant_msg:
|
| 43 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 44 |
+
|
| 45 |
+
messages.append({"role": "user", "content": message})
|
| 46 |
+
return _backend.chat(messages)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def build_demo() -> gr.Blocks:
|
| 50 |
+
model_repo = os.environ.get("MODEL_REPO", "Qwen/Qwen2.5-3B-Instruct-GGUF")
|
| 51 |
+
model_file = os.environ.get("MODEL_FILE", "qwen2.5-3b-instruct-q4_k_m.gguf")
|
| 52 |
+
backend_name = os.environ.get("INFERENCE_BACKEND", "llama_cpp")
|
| 53 |
+
|
| 54 |
+
with gr.Blocks(title="Small Model Hackathon") as demo:
|
| 55 |
+
gr.Markdown(
|
| 56 |
+
f"""
|
| 57 |
+
# Small Model Chat
|
| 58 |
+
|
| 59 |
+
Local inference via **{backend_name}**. Model loads on first message.
|
| 60 |
+
|
| 61 |
+
- **Repo:** `{model_repo}`
|
| 62 |
+
- **File:** `{model_file}`
|
| 63 |
+
|
| 64 |
+
Part of the [Build Small Hackathon](https://huggingface.co/build-small-hackathon).
|
| 65 |
+
"""
|
| 66 |
+
)
|
| 67 |
+
gr.ChatInterface(
|
| 68 |
+
fn=chat,
|
| 69 |
+
examples=["Hello! What can you help me with?", "Explain llama.cpp in one sentence."],
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
return demo
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
demo = build_demo()
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def main() -> None:
|
| 79 |
+
demo.launch(
|
| 80 |
+
server_name="0.0.0.0",
|
| 81 |
+
server_port=int(os.environ.get("PORT", "7860")),
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
main()
|