Spaces:
Runtime error
Runtime error
File size: 2,717 Bytes
aa92d0c 2900589 aa92d0c 1bfdb58 2900589 aa92d0c 2900589 aa92d0c 2900589 aa92d0c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import gradio as gr
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import torch
# ----------------------------
# 1. MODEL YÜKLEME (Lazy)
# ----------------------------
model = None
tokenizer = None
def model_yukle():
global model, tokenizer
if model is not None:
return
from transformers import AutoModelForCausalLM, AutoTokenizer
print("🔄 Model yükleniyor...")
tokenizer = AutoTokenizer.from_pretrained(
"VoidOaz/Timur1Flash",
trust_remote_code=True,
)
model = AutoModelForCausalLM.from_pretrained(
"VoidOaz/Timur1Flash",
dtype=torch.float32,
device_map="cpu",
trust_remote_code=True,
)
print("✅ Model hazır")
# ----------------------------
# 2. GRADIO ARAYÜZÜ (Sadece "Running" olması için)
# ----------------------------
def gradio_sohbet(mesaj, gecmis):
model_yukle()
# (Burada da modeli çalıştırabilirsin, ama Vercel için gerek yok)
return "Bu arayüz test amaçlıdır. Vercel'den /sohbet endpoint'ini kullanın."
with gr.Blocks() as demo:
gr.Markdown("# 🤖 Timur 1 Flash")
gr.Markdown("Vercel'den `/sohbet` endpoint'ine POST isteği atın.")
chatbot = gr.Chatbot()
msg = gr.Textbox(placeholder="Mesaj yaz...")
msg.submit(gradio_sohbet, [msg, chatbot], [chatbot])
# ----------------------------
# 3. FASTAPI + CORS + ÖZEL ENDPOINT
# ----------------------------
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Vercel domain'ini sonra kısıtlayabilirsin
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class Soru(BaseModel):
mesaj: str
@app.post("/sohbet")
async def sohbet(soru: Soru):
model_yukle()
mesajlar = [
{"role": "system", "content": "Sen Timur AI adında zeki ve yardımcı bir yapay zeka asistanısın."},
{"role": "user", "content": soru.mesaj},
]
metin = tokenizer.apply_chat_template(mesajlar, tokenize=False, add_generation_prompt=True)
girdi = tokenizer(metin, return_tensors="pt")
with torch.no_grad():
cikti = model.generate(
**girdi,
max_new_tokens=150,
do_sample=True,
temperature=0.7,
pad_token_id=tokenizer.eos_token_id,
)
cevap = tokenizer.decode(cikti[0][girdi["input_ids"].shape[1]:], skip_special_tokens=True)
return {"cevap": cevap.strip()}
@app.get("/")
def root():
return {"durum": "Timur 1 Flash çalışıyor"}
# ----------------------------
# 4. GRADIO'YU FASTAPI'YE MOUNT ET
# ----------------------------
app = gr.mount_gradio_app(app, demo, path="/") |