Javedalam commited on
Commit
98b445f
Β·
verified Β·
1 Parent(s): 93545df

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+ HF_URL = "http://localhost:7860/completion" # llama-server inside container
6
+
7
+ def translate(text, direction):
8
+ if direction == "English β†’ Japanese":
9
+ prompt = f"Translate to Japanese:\n{text}\nJapanese:"
10
+ else:
11
+ prompt = f"Translate to English:\n{text}\nEnglish:"
12
+
13
+ resp = requests.post(
14
+ HF_URL,
15
+ json={
16
+ "prompt": prompt,
17
+ "n_predict": 128,
18
+ "temperature": 0.1,
19
+ "top_p": 0.9,
20
+ "repeat_penalty": 1.3,
21
+ "stop": ["\n\n","</s>"]
22
+ }
23
+ )
24
+ return resp.json().get("content", "")
25
+
26
+ with gr.Blocks() as demo:
27
+ gr.Markdown("# πŸ‡¬πŸ‡§β‡„πŸ‡―πŸ‡΅ English ↔ Japanese Translator (llama.cpp)")
28
+ with gr.Row():
29
+ with gr.Column():
30
+ direction = gr.Radio(["English β†’ Japanese", "Japanese β†’ English"], value="English β†’ Japanese")
31
+ input_box = gr.Textbox(label="Input")
32
+ output_box = gr.Textbox(label="Translation")
33
+ run_btn = gr.Button("Translate")
34
+ run_btn.click(fn=translate, inputs=[input_box, direction], outputs=output_box)
35
+
36
+ demo.launch(server_name="0.0.0.0", server_port=7860)