|
|
import gradio as gr |
|
|
from fastapi import FastAPI, Request |
|
|
from fastapi.responses import JSONResponse |
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
import os |
|
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
app = FastAPI() |
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins=["*"], |
|
|
allow_credentials=True, |
|
|
allow_methods=["*"], |
|
|
allow_headers=["*"], |
|
|
) |
|
|
|
|
|
|
|
|
client = InferenceClient(token=os.getenv("HF_TOKEN")) |
|
|
|
|
|
@app.post("/api/chat") |
|
|
async def chat_api(request: Request): |
|
|
try: |
|
|
data = await request.json() |
|
|
user_msg = data.get("message", "") |
|
|
history = data.get("history", []) |
|
|
|
|
|
SYSTEM_PROMPT = """Sen ZenkaMind 1.1'sin. Tek Yaratıcın: Berkay Adıgül. |
|
|
TÜRKÇE konuş. Mahallenin ağır abisi karakterindesin.""" |
|
|
|
|
|
messages = [{"role": "system", "content": SYSTEM_PROMPT}] |
|
|
|
|
|
for msg in history: |
|
|
if len(msg) >= 2: |
|
|
messages.append({"role": "user", "content": str(msg[0])}) |
|
|
messages.append({"role": "assistant", "content": str(msg[1])}) |
|
|
|
|
|
messages.append({"role": "user", "content": user_msg}) |
|
|
|
|
|
full_response = "" |
|
|
stream = client.chat_completion( |
|
|
model="Qwen/Qwen2.5-7B-Instruct", |
|
|
messages=messages, |
|
|
max_tokens=500, |
|
|
stream=True, |
|
|
temperature=0.7 |
|
|
) |
|
|
|
|
|
for chunk in stream: |
|
|
if chunk.choices and chunk.choices[0].delta.content: |
|
|
full_response += chunk.choices[0].delta.content |
|
|
|
|
|
return JSONResponse({"response": full_response}) |
|
|
|
|
|
except Exception as e: |
|
|
return JSONResponse({"response": f"⚠️ Sistem meşgul: {str(e)}"}) |
|
|
|
|
|
|
|
|
def simple_chat(message, history): |
|
|
return "HTML arayüzü için /api/chat endpoint'ini kullanın" |
|
|
|
|
|
demo = gr.ChatInterface( |
|
|
simple_chat, |
|
|
title="ZenkaMind", |
|
|
description="Ana uygulama için HTML frontend kullanın" |
|
|
) |
|
|
|
|
|
app = gr.mount_gradio_app(app, demo, path="/") |
|
|
|
|
|
@app.get("/") |
|
|
async def home(): |
|
|
return {"status": "ZenkaMind API çalışıyor", "endpoint": "/api/chat"} |
|
|
|
|
|
if __name__ == "__main__": |
|
|
import uvicorn |
|
|
uvicorn.run(app, host="0.0.0.0", port=7860) |