Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,36 @@
|
|
| 1 |
-
import
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import pipeline
|
| 5 |
|
|
|
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
pipe =
|
| 10 |
|
| 11 |
class GenerateRequest(BaseModel):
|
| 12 |
prompt: str
|
| 13 |
|
|
|
|
| 14 |
@app.post("/api/generate")
|
| 15 |
def generate(request: GenerateRequest):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
output = pipe(request.prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 17 |
return {"response": output[0]['generated_text']}
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# 1. Hugging Face'in otomatik tanıyacağı ana FastAPI objemiz
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# 2. Modeli başlangıçta YÜKLEMİYORUZ (SSR Timeout yemesin diye)
|
| 10 |
+
pipe = None
|
| 11 |
|
| 12 |
class GenerateRequest(BaseModel):
|
| 13 |
prompt: str
|
| 14 |
|
| 15 |
+
# 3. Kendi API endpoint'imiz
|
| 16 |
@app.post("/api/generate")
|
| 17 |
def generate(request: GenerateRequest):
|
| 18 |
+
global pipe
|
| 19 |
+
# İlk POST isteği geldiğinde modeli belleğe alıyoruz
|
| 20 |
+
if pipe is None:
|
| 21 |
+
print("Model ilk istek için yükleniyor...")
|
| 22 |
+
pipe = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B-Instruct", device="cpu")
|
| 23 |
+
|
| 24 |
output = pipe(request.prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 25 |
return {"response": output[0]['generated_text']}
|
| 26 |
|
| 27 |
+
# 4. Hugging Face sistemini kandırmak için göstermelik (dummy) bir arayüz
|
| 28 |
+
def dummy_ui(text):
|
| 29 |
+
return "UI devre dışı. İstekleri /api/generate adresine POST atın."
|
| 30 |
+
|
| 31 |
+
demo = gr.Interface(fn=dummy_ui, inputs="text", outputs="text")
|
| 32 |
+
|
| 33 |
+
# 5. Gradio arayüzünü FastAPI içine monte ediyoruz (HF sistemi bunu görünce rahatlayacak)
|
| 34 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 35 |
+
|
| 36 |
+
# DİKKAT: uvicorn.run() YAZMIYORUZ! Ayağa kaldırma işini HF'ye bırakıyoruz.
|