Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,14 @@ from huggingface_hub import InferenceClient
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
app.add_middleware(
|
| 10 |
-
CORSMiddleware,
|
| 11 |
-
allow_origins=["*"],
|
| 12 |
-
allow_credentials=True,
|
| 13 |
-
allow_methods=["*"],
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
|
|
|
| 17 |
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 18 |
|
| 19 |
@app.post("/api/chat")
|
|
@@ -21,10 +22,19 @@ async def chat_api(request: Request):
|
|
| 21 |
try:
|
| 22 |
data = await request.json()
|
| 23 |
user_msg = data.get("message", "")
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
full_response = ""
|
| 30 |
stream = client.chat_completion(
|
|
@@ -42,19 +52,24 @@ async def chat_api(request: Request):
|
|
| 42 |
return JSONResponse({"response": full_response})
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
-
return JSONResponse({"response": f"
|
| 46 |
|
| 47 |
-
# Gradio arayüzü
|
| 48 |
-
def
|
| 49 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
|
| 52 |
-
app = gr.mount_gradio_app(app, io, path="/")
|
| 53 |
|
| 54 |
@app.get("/")
|
| 55 |
async def home():
|
| 56 |
-
return {"status": "ZenkaMind API çalışıyor"}
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
import uvicorn
|
| 60 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
app.add_middleware(
|
| 10 |
+
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"],
|
| 12 |
+
allow_credentials=True,
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# HF Client - Token environment variable'dan alınacak
|
| 18 |
client = InferenceClient(token=os.getenv("HF_TOKEN"))
|
| 19 |
|
| 20 |
@app.post("/api/chat")
|
|
|
|
| 22 |
try:
|
| 23 |
data = await request.json()
|
| 24 |
user_msg = data.get("message", "")
|
| 25 |
+
history = data.get("history", [])
|
| 26 |
|
| 27 |
+
SYSTEM_PROMPT = """Sen ZenkaMind 1.1'sin. Tek Yaratıcın: Berkay Adıgül.
|
| 28 |
+
TÜRKÇE konuş. Mahallenin ağır abisi karakterindesin."""
|
| 29 |
+
|
| 30 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 31 |
+
|
| 32 |
+
for msg in history:
|
| 33 |
+
if len(msg) >= 2:
|
| 34 |
+
messages.append({"role": "user", "content": str(msg[0])})
|
| 35 |
+
messages.append({"role": "assistant", "content": str(msg[1])})
|
| 36 |
+
|
| 37 |
+
messages.append({"role": "user", "content": user_msg})
|
| 38 |
|
| 39 |
full_response = ""
|
| 40 |
stream = client.chat_completion(
|
|
|
|
| 52 |
return JSONResponse({"response": full_response})
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
+
return JSONResponse({"response": f"⚠️ Sistem meşgul: {str(e)}"})
|
| 56 |
|
| 57 |
+
# Basit Gradio arayüzü
|
| 58 |
+
def simple_chat(message, history):
|
| 59 |
+
return "HTML arayüzü için /api/chat endpoint'ini kullanın"
|
| 60 |
+
|
| 61 |
+
demo = gr.ChatInterface(
|
| 62 |
+
simple_chat,
|
| 63 |
+
title="ZenkaMind",
|
| 64 |
+
description="Ana uygulama için HTML frontend kullanın"
|
| 65 |
+
)
|
| 66 |
|
| 67 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 68 |
|
| 69 |
@app.get("/")
|
| 70 |
async def home():
|
| 71 |
+
return {"status": "ZenkaMind API çalışıyor", "endpoint": "/api/chat"}
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
import uvicorn
|
| 75 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|