Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,15 @@
|
|
| 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 |
-
|
| 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(
|
| 25 |
-
return
|
| 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 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
pipe = None
|
| 5 |
|
| 6 |
+
def generate_response(prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
global pipe
|
|
|
|
| 8 |
if pipe is None:
|
|
|
|
| 9 |
pipe = pipeline("text-generation", model="Qwen/Qwen2.5-0.5B-Instruct", device="cpu")
|
| 10 |
+
|
| 11 |
+
output = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 12 |
+
return output[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
demo = gr.Interface(fn=generate_response, inputs="text", outputs="text")
|
| 15 |
+
demo.launch()
|