PsychoNay commited on
Commit
51a8967
·
verified ·
1 Parent(s): 5c2a5e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -3
app.py CHANGED
@@ -2,6 +2,7 @@ from fastapi import FastAPI, HTTPException
2
  from pydantic import BaseModel
3
  import gradio as gr
4
  from deep_translator import GoogleTranslator
 
5
 
6
  # --- 1. API အတွက် Input Model ကိုသတ်မှတ်ပါ ---
7
  class TranslationRequest(BaseModel):
@@ -21,7 +22,6 @@ def get_translation(text: str) -> str:
21
  raise HTTPException(status_code=500, detail=f"Translation error: {str(e)}")
22
 
23
  # --- 3. FastAPI App ကိုတည်ဆောက်ပြီး API Endpoint ဖန်တီးပါ ---
24
- # ဒါက သင့်ရဲ့ Flask API နဲ့ ဆင်တူပါတယ်
25
  api = FastAPI()
26
 
27
  @api.post("/api/translate")
@@ -37,7 +37,6 @@ def handle_api_translation(request: TranslationRequest):
37
  }
38
 
39
  # --- 4. Gradio UI ကိုတည်ဆောက်ပါ ---
40
- # ဒီ UI ကလည်း အပေါ်က get_translation function ကိုပဲ ပြန်သုံးမှာပါ
41
  def gradio_translate_wrapper(text: str):
42
  """Gradio အတွက် error handling သီးသန့်လုပ်ထားတဲ့ wrapper"""
43
  try:
@@ -54,6 +53,10 @@ gradio_ui = gr.Interface(
54
  )
55
 
56
  # --- 5. FastAPI app ပေါ်မှာ Gradio UI ကို mount လုပ်ပါ ---
57
- # Space က ဒီ app object ကို အဓိကထားပြီး run မှာဖြစ်ပါတယ်
58
  app = gr.mount_gradio_app(api, gradio_ui, path="/")
59
 
 
 
 
 
 
 
2
  from pydantic import BaseModel
3
  import gradio as gr
4
  from deep_translator import GoogleTranslator
5
+ import uvicorn # uvicorn ကို import လုပ်ပါ
6
 
7
  # --- 1. API အတွက် Input Model ကိုသတ်မှတ်ပါ ---
8
  class TranslationRequest(BaseModel):
 
22
  raise HTTPException(status_code=500, detail=f"Translation error: {str(e)}")
23
 
24
  # --- 3. FastAPI App ကိုတည်ဆောက်ပြီး API Endpoint ဖန်တီးပါ ---
 
25
  api = FastAPI()
26
 
27
  @api.post("/api/translate")
 
37
  }
38
 
39
  # --- 4. Gradio UI ကိုတည်ဆောက်ပါ ---
 
40
  def gradio_translate_wrapper(text: str):
41
  """Gradio အတွက် error handling သီးသန့်လုပ်ထားတဲ့ wrapper"""
42
  try:
 
53
  )
54
 
55
  # --- 5. FastAPI app ပေါ်မှာ Gradio UI ကို mount လုပ်ပါ ---
 
56
  app = gr.mount_gradio_app(api, gradio_ui, path="/")
57
 
58
+ # --- 6. Server ကို တိုက်ရိုက် run ရန်အတွက် main block ထည့်သွင်းပါ ---
59
+ # Hugging Face Space runner က uvicorn ကို အလိုအလျောက် run ပေးပေမယ့်၊
60
+ # ဒီ block က startup ပြဿနာအချို့ကို ဖြေရှင်းပေးနိုင်ပြီး app ကို အမြဲတမ်း run နေစေဖို့ သေချာစေပါတယ်။
61
+ if __name__ == "__main__":
62
+ uvicorn.run(app, host="0.0.0.0", port=7860)