Spaces:
Running on Zero
Running on Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
from llama_cpp import Llama
|
| 8 |
+
|
| 9 |
+
# lukey03-এর Qwen 3.5 9B Abliterated GGUF মডেল কনফিগারেশন
|
| 10 |
+
MODEL_REPO = "lukey03/Qwen3.5-9B-abliterated-GGUF"
|
| 11 |
+
MODEL_FILE = "Qwen3.5-9B-abliterated-Q4_K_M.gguf"
|
| 12 |
+
|
| 13 |
+
print(f"Downloading model: {MODEL_REPO}/{MODEL_FILE}...")
|
| 14 |
+
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILE)
|
| 15 |
+
|
| 16 |
+
print("Loading model into llama-cpp...")
|
| 17 |
+
# CPU এনভায়রনমেন্টের জন্য অপটিমাইজড পারফরম্যান্স ফ্ল্যাগস
|
| 18 |
+
llm = Llama(
|
| 19 |
+
model_path=model_path,
|
| 20 |
+
n_ctx=4096,
|
| 21 |
+
n_threads=4,
|
| 22 |
+
n_batch=512,
|
| 23 |
+
verbose=False
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
app = FastAPI()
|
| 27 |
+
|
| 28 |
+
# ১. Void AI / Cursor-এর জন্য OpenAI Compatible Endpoint
|
| 29 |
+
@app.post("/v1/chat/completions")
|
| 30 |
+
async def chat_completions(request: Request):
|
| 31 |
+
try:
|
| 32 |
+
body = await request.json()
|
| 33 |
+
messages = body.get("messages", [])
|
| 34 |
+
max_tokens = body.get("max_tokens", 2048)
|
| 35 |
+
temperature = body.get("temperature", 0.2)
|
| 36 |
+
stream = body.get("stream", False)
|
| 37 |
+
|
| 38 |
+
# Non-streaming Response
|
| 39 |
+
if not stream:
|
| 40 |
+
response = llm.create_chat_completion(
|
| 41 |
+
messages=messages,
|
| 42 |
+
max_tokens=max_tokens,
|
| 43 |
+
temperature=temperature
|
| 44 |
+
)
|
| 45 |
+
return JSONResponse(content=response)
|
| 46 |
+
|
| 47 |
+
# Streaming Response (যদি এডিটর স্ট্রিম রিকোয়েস্ট পাঠায়)
|
| 48 |
+
def event_stream():
|
| 49 |
+
stream_resp = llm.create_chat_completion(
|
| 50 |
+
messages=messages,
|
| 51 |
+
max_tokens=max_tokens,
|
| 52 |
+
temperature=temperature,
|
| 53 |
+
stream=True
|
| 54 |
+
)
|
| 55 |
+
for chunk in stream_resp:
|
| 56 |
+
yield f"data: {json.dumps(chunk)}\n\n"
|
| 57 |
+
yield "data: [DONE]\n\n"
|
| 58 |
+
|
| 59 |
+
return StreamingResponse(event_stream(), media_type="text/event-stream")
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 63 |
+
|
| 64 |
+
# ২. ব্রাউজার ও সরাসরি চ্যাটের জন্য Gradio Web UI
|
| 65 |
+
def generate_code(message, history):
|
| 66 |
+
formatted_messages = []
|
| 67 |
+
|
| 68 |
+
# পূর্ববর্তী ইতিহাস সঠিকভাবে সাজানো
|
| 69 |
+
for item in history:
|
| 70 |
+
if isinstance(item, (list, tuple)) and len(item) == 2:
|
| 71 |
+
formatted_messages.append({"role": "user", "content": item[0]})
|
| 72 |
+
formatted_messages.append({"role": "assistant", "content": item[1]})
|
| 73 |
+
|
| 74 |
+
formatted_messages.append({"role": "user", "content": message})
|
| 75 |
+
|
| 76 |
+
response = llm.create_chat_completion(
|
| 77 |
+
messages=formatted_messages,
|
| 78 |
+
max_tokens=2048,
|
| 79 |
+
temperature=0.2
|
| 80 |
+
)
|
| 81 |
+
return response['choices'][0]['message']['content']
|
| 82 |
+
|
| 83 |
+
demo = gr.ChatInterface(
|
| 84 |
+
fn=generate_code,
|
| 85 |
+
title="Qwen 3.5 9B Abliterated - Universal Coding API"
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Gradio ইন্টারফেসকে FastAPI অ্যাপে মাউন্ট করা
|
| 89 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|