Juna190825 commited on
Commit
c8d8296
·
verified ·
1 Parent(s): d08f4a9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -35
app.py CHANGED
@@ -1,42 +1,45 @@
1
  import gradio as gr
2
-
 
3
  from gradio_client import Client
 
 
 
4
 
 
 
 
 
 
 
 
 
5
 
6
- def chat(message):
7
- # return f"You said: {message}"
8
- msg = f"Translate Zomi to English: '{message}'"
9
- client = Client("Chatboong/Gemini_Translator") # working as of 20251125,
10
  result = client.predict(
11
- message=msg,
12
- lang="English",
13
- is_streaming=True,
14
- api_name="/chat"
15
  )
16
  return result
17
- # try:
18
- # client = Client("Chatboong/Gemini_Translator")
19
- # result = client.predict(
20
- # message=msg,
21
- # lang="English",
22
- # is_streaming=True,
23
- # api_name="chat"
24
- # )
25
- # return result
26
- # except Exception as e:
27
- # return f"Upstream error: {e}"
28
-
29
-
30
- with gr.Blocks() as demo:
31
- gr.Markdown("### Zomi Translator")
32
- inp = gr.Textbox(label="Input")
33
- out = gr.Textbox(label="Output")
34
- inp.submit(chat, inp, out)
35
- demo.load(
36
- fn=chat,
37
- inputs=gr.Textbox(),
38
- outputs=gr.Textbox(),
39
- api_name="/chat"
40
- )
41
-
42
- demo.launch()
 
1
  import gradio as gr
2
+ from fastapi import FastAPI
3
+ from fastapi.middleware.cors import CORSMiddleware
4
  from gradio_client import Client
5
+ import uvicorn
6
+
7
+ app = FastAPI()
8
 
9
+ # Enable CORS
10
+ app.add_middleware(
11
+ CORSMiddleware,
12
+ allow_origins=["*"], # or restrict to specific domains
13
+ allow_credentials=True,
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
 
18
+ def translate_zomi(text: str):
19
+ msg = f"Translate Zomi to English: '{text}'"
20
+ client = Client("Chatboong/Gemini_Translator")
 
21
  result = client.predict(
22
+ message=msg,
23
+ lang="English",
24
+ is_streaming=True,
25
+ api_name="/chat"
26
  )
27
  return result
28
+
29
+ @app.post("/chat")
30
+ async def chat_api(message: str):
31
+ return {"translation": translate_zomi(message)}
32
+
33
+ def chat_ui(message):
34
+ return translate_zomi(message)
35
+
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown("### Zomi Translator")
38
+ inp = gr.Textbox(label="Input")
39
+ out = gr.Textbox(label="Output")
40
+ inp.submit(chat_ui, inp, out)
41
+
42
+ app = gr.mount_gradio_app(app, demo, path="/")
43
+
44
+ if __name__ == "__main__":
45
+ uvicorn.run(app, host="0.0.0.0", port=7860)