| import os |
| import uvicorn |
| from fastapi import FastAPI, HTTPException |
| from pydantic import BaseModel |
| from typing import List, Dict |
| import gradio as gr |
| from huggingface_hub import hf_hub_download |
| from llama_cpp import Llama |
|
|
| |
| |
| |
| |
| repo_id = "bartowski/Qwen2.5-7B-Instruct-GGUF" |
| filename = "Qwen2.5-7B-Instruct-Q4_K_M.gguf" |
|
|
| print("Downloading/Verifying 4-bit GGUF model...") |
| model_path = hf_hub_download(repo_id=repo_id, filename=filename) |
|
|
| |
| llm = Llama( |
| model_path=model_path, |
| n_ctx=2048, |
| n_threads=2, |
| verbose=False |
| ) |
|
|
| |
| |
| |
| app = FastAPI(title="Qwen 7B API & UI Space") |
|
|
| class ChatRequest(BaseModel): |
| messages: List[Dict[str, str]] |
| max_tokens: int = 512 |
| temperature: float = 0.7 |
|
|
| @app.post("/api/chat") |
| async def api_chat(request: ChatRequest): |
| """ |
| API Endpoint for external access. |
| Expects ChatML format memory/history. |
| """ |
| try: |
| |
| prompt = "<|im_start|>system\nYou are a helpful AI assistant.<|im_end|>\n" |
| for msg in request.messages: |
| role = msg.get("role", "user") |
| content = msg.get("content", "") |
| prompt += f"<|im_start|>{role}\n{content}<|im_end|>\n" |
| |
| prompt += "<|im_start|>assistant\n" |
|
|
| |
| output = llm( |
| prompt, |
| max_tokens=request.max_tokens, |
| stop=["<|im_end|>", "<|im_start|>"], |
| temperature=request.temperature, |
| top_p=0.9 |
| ) |
| |
| return { |
| "reply": output['choices'][0]['text'].strip(), |
| "usage": output.get('usage', {}) |
| } |
| except Exception as e: |
| raise HTTPException(status_code=500, detail=str(e)) |
|
|
| |
| |
| |
| def chat_stream(message, history): |
| """ |
| Handles the Gradio UI chat. 'history' acts as session memory. |
| """ |
| prompt = "<|im_start|>system\nYou are a helpful AI assistant.<|im_end|>\n" |
| |
| |
| for user_msg, bot_msg in history: |
| prompt += f"<|im_start|>user\n{user_msg}<|im_end|>\n" |
| prompt += f"<|im_start|>assistant\n{bot_msg}<|im_end|>\n" |
| |
| |
| prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n" |
| |
| |
| stream = llm( |
| prompt, |
| max_tokens=512, |
| stop=["<|im_end|>", "<|im_start|>"], |
| stream=True, |
| temperature=0.7 |
| ) |
| |
| response = "" |
| for chunk in stream: |
| token = chunk['choices'][0]['text'] |
| response += token |
| yield response |
|
|
| |
| ui = gr.ChatInterface( |
| fn=chat_stream, |
| title="Qwen 2.5 7B (4-bit GGUF)", |
| description="UI features per-user session memory. API is available at `/api/chat`." |
| ) |
|
|
| |
| |
| |
| |
| app = gr.mount_gradio_app(app, ui, path="/") |
|
|
| if __name__ == "__main__": |
| |
| uvicorn.run(app, host="0.0.0.0", port=7860) |