plan291037 commited on
Commit
fd7fe2c
·
verified ·
1 Parent(s): 9ecc5ea

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Query
2
+ from openai import OpenAI
3
+
4
+ # ชี้ client ไปที่ Ollama ภายในคอนเทนเนอร์
5
+ client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
6
+ MODEL = "scb10x/typhoon-translate-4b" # ไทย↔อังกฤษ เน้นเร็ว-แม่น
7
+
8
+ app = FastAPI(title="HF Space · Ollama Translator")
9
+
10
+ SYSTEM_TMPL = (
11
+ "You are a professional MT engine. Translate {src}→{tgt}. "
12
+ "Return ONLY the translation without extra text."
13
+ )
14
+
15
+ @app.get("/translate")
16
+ def translate(
17
+ text: str = Query(..., description="ข้อความต้นฉบับ"),
18
+ target: str = Query("en", description="ภาษาปลายทาง เช่น en, th"),
19
+ source: str = Query("auto", description="ภาษาต้นทาง (auto ได้)")
20
+ ):
21
+ system_msg = SYSTEM_TMPL.format(src=source, tgt=target)
22
+ resp = client.chat.completions.create(
23
+ model=MODEL,
24
+ messages=[
25
+ {"role": "system", "content": system_msg},
26
+ {"role": "user", "content": text},
27
+ ],
28
+ temperature=0,
29
+ stream=False,
30
+ )
31
+ out = resp.choices[0].message.content.strip()
32
+ return {"translation": out, "model": MODEL}