import gradio as gr import requests import os HF_URL = "http://localhost:7860/completion" # llama-server inside container def translate(text, direction): if direction == "English โ†’ Japanese": prompt = f"Translate to Japanese:\n{text}\nJapanese:" else: prompt = f"Translate to English:\n{text}\nEnglish:" resp = requests.post( HF_URL, json={ "prompt": prompt, "n_predict": 128, "temperature": 0.1, "top_p": 0.9, "repeat_penalty": 1.3, "stop": ["\n\n",""] } ) return resp.json().get("content", "") with gr.Blocks() as demo: gr.Markdown("# ๐Ÿ‡ฌ๐Ÿ‡งโ‡„๐Ÿ‡ฏ๐Ÿ‡ต English โ†” Japanese Translator (llama.cpp)") with gr.Row(): with gr.Column(): direction = gr.Radio(["English โ†’ Japanese", "Japanese โ†’ English"], value="English โ†’ Japanese") input_box = gr.Textbox(label="Input") output_box = gr.Textbox(label="Translation") run_btn = gr.Button("Translate") run_btn.click(fn=translate, inputs=[input_box, direction], outputs=output_box) demo.launch(server_name="0.0.0.0", server_port=7860)