ciyidogan commited on
Commit
444adae
·
verified ·
1 Parent(s): 1e913ae

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import time
4
+ import threading
5
+ from datetime import datetime
6
+ from fastapi import FastAPI
7
+ from pydantic import BaseModel
8
+ from transformers import pipeline
9
+ from unsloth import FastLanguageModel
10
+
11
+ # === Ortam değişkenleri
12
+ os.environ.setdefault("HF_HOME", "/app/.cache")
13
+ os.environ.setdefault("HF_HUB_CACHE", "/app/.cache")
14
+ os.environ.setdefault("BITSANDBYTES_NOWELCOME", "1")
15
+
16
+ # === Basit log
17
+ def log(message):
18
+ timestamp = datetime.now().strftime("%H:%M:%S")
19
+ print(f"[{timestamp}] {message}", flush=True)
20
+
21
+ # === FastAPI başlat
22
+ app = FastAPI()
23
+ pipe = None
24
+
25
+ @app.on_event("startup")
26
+ def load_model():
27
+ global pipe
28
+ model_name = "atasoglu/Turkish-Llama-3-8B-function-calling"
29
+ log(f"⬇️ Model yükleniyor: {model_name}")
30
+ model, tokenizer = FastLanguageModel.from_pretrained(
31
+ model_name=model_name,
32
+ load_in_4bit=True,
33
+ device_map="auto"
34
+ )
35
+ FastLanguageModel.for_inference(model)
36
+ pipe = pipeline(
37
+ "text-generation",
38
+ model=model,
39
+ tokenizer=tokenizer,
40
+ device_map="auto"
41
+ )
42
+ log("✅ Model yüklendi, test etmeye hazır.")
43
+
44
+ class TestRequest(BaseModel):
45
+ user_input: str
46
+
47
+ @app.post("/test")
48
+ def test(req: TestRequest):
49
+ prompt = f"Kullanıcı: {req.user_input}\nAsistan:"
50
+ log(f"💬 Prompt alındı: {req.user_input}")
51
+ outputs = pipe(
52
+ prompt,
53
+ max_new_tokens=256,
54
+ temperature=0.2,
55
+ top_p=0.95,
56
+ repetition_penalty=1.1
57
+ )
58
+ answer = outputs[0]["generated_text"].replace(prompt, "").strip()
59
+ log("✅ Cevap üretildi.")
60
+ return {"response": answer}
61
+
62
+ @app.get("/")
63
+ def health():
64
+ return {"status": "ok"}
65
+
66
+ def run_health_server():
67
+ import uvicorn
68
+ uvicorn.run(app, host="0.0.0.0", port=7860)
69
+
70
+ threading.Thread(target=run_health_server, daemon=True).start()
71
+
72
+ log("⏸️ Uygulama bekleme modunda...")
73
+ while True:
74
+ time.sleep(60)