ghosttech commited on
Commit
2a0ccb8
·
verified ·
1 Parent(s): 9842d02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -27
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
- 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.
 
 
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()