| import os |
| import json |
| import gradio as gr |
| import spaces |
| from fastapi import Request, Response |
| from fastapi.responses import JSONResponse, StreamingResponse |
| from huggingface_hub import hf_hub_download |
| from llama_cpp import Llama |
|
|
| |
| @spaces.GPU(duration=5) |
| def dummy_gpu(): |
| print("ZeroGPU validation satisfied.") |
| return 1 |
|
|
| dummy_gpu() |
|
|
| |
| print("Downloading LFM2.5-8B-A1B-GGUF model...") |
| model_path = hf_hub_download( |
| repo_id="Abiray/MiniCPM5-1B-GGUF", |
| filename="minicpm5-1b-Q6_K.gguf" |
| ) |
|
|
| |
| print("Loading model into memory via llama-cpp-python...") |
| llm = Llama( |
| model_path=model_path, |
| n_ctx=8192, |
| n_threads=8, |
| verbose=False |
| ) |
| print("Model loaded successfully!") |
|
|
| |
| def chat_with_llama(message, history): |
| messages = [{"role": "system", "content": "You are a helpful AI assistant."}] |
| for user_msg, assistant_msg in history: |
| messages.append({"role": "user", "content": user_msg}) |
| messages.append({"role": "assistant", "content": assistant_msg}) |
| |
| messages.append({"role": "user", "content": message}) |
| |
| stream = llm.create_chat_completion( |
| messages=messages, |
| stream=True, |
| temperature=0.7, |
| max_tokens=1024 |
| ) |
| |
| partial_response = "" |
| for chunk in stream: |
| if "choices" in chunk and len(chunk["choices"]) > 0: |
| delta = chunk["choices"][0].get("delta", {}) |
| if "content" in delta: |
| partial_response += delta["content"] |
| yield partial_response |
|
|
| with gr.Blocks() as demo: |
| gr.Markdown("# ๐ LiquidAI LFM2.5-8B (CPU & API Enabled)") |
| gr.Markdown(""" |
| Running **LFM2.5-8B-A1B** natively via `llama-cpp-python` entirely on CPU! ZeroGPU is bypassed safely. |
| |
| ### ๐ Developer API Available! |
| This Space also silently hosts a real API endpoint on the exact same port. |
| **Endpoint:** `POST /v1/chat/completions` (Supports Streaming & Non-Streaming OpenAI formats) |
| """) |
| |
| gr.ChatInterface( |
| fn=chat_with_llama, |
| examples=["Who are you?", "Write a python script to reverse a string.", "Explain quantum computing."], |
| ) |
|
|
| |
| import fastapi |
|
|
| |
| original_fastapi_init = fastapi.FastAPI.__init__ |
|
|
| def custom_fastapi_init(self, *args, **kwargs): |
| |
| original_fastapi_init(self, *args, **kwargs) |
| |
| |
| if kwargs.get("title") == "Gradio" or "gradio" in str(args): |
| print("Successfully targeted Gradio's internal FastAPI server! Injecting routes...") |
| |
| |
| @self.options("/v1/chat/completions") |
| async def chat_api_options(): |
| return Response( |
| status_code=200, |
| headers={ |
| "Access-Control-Allow-Origin": "*", |
| "Access-Control-Allow-Methods": "POST, OPTIONS", |
| "Access-Control-Allow-Headers": "Content-Type, Authorization", |
| } |
| ) |
| |
| |
| @self.post("/v1/chat/completions") |
| async def chat_api(request: Request): |
| try: |
| body = await request.json() |
| messages = body.get("messages", []) |
| stream = body.get("stream", False) |
| temperature = body.get("temperature", 0.7) |
| max_tokens = body.get("max_tokens", 1024) |
| |
| headers = { |
| "Access-Control-Allow-Origin": "*", |
| "Access-Control-Allow-Methods": "POST, OPTIONS", |
| "Access-Control-Allow-Headers": "Content-Type, Authorization", |
| } |
| |
| if stream: |
| def stream_generator(): |
| for chunk in llm.create_chat_completion( |
| messages=messages, |
| stream=True, |
| temperature=temperature, |
| max_tokens=max_tokens |
| ): |
| yield f"data: {json.dumps(chunk)}\n\n" |
| yield "data: [DONE]\n\n" |
| return StreamingResponse( |
| stream_generator(), |
| media_type="text/event-stream", |
| headers=headers |
| ) |
| else: |
| response = llm.create_chat_completion( |
| messages=messages, |
| stream=False, |
| temperature=temperature, |
| max_tokens=max_tokens |
| ) |
| return JSONResponse(content=response, headers=headers) |
| except Exception as e: |
| return JSONResponse( |
| status_code=500, |
| content={"error": str(e)}, |
| headers={"Access-Control-Allow-Origin": "*"} |
| ) |
|
|
| |
| fastapi.FastAPI.__init__ = custom_fastapi_init |
|
|
| |
| if __name__ == "__main__": |
| demo.launch(server_name="0.0.0.0", server_port=7860) |