|
import gradio as gr |
|
import requests |
|
import urllib.parse |
|
import re |
|
from fastapi import FastAPI, Request, HTTPException |
|
import uvicorn |
|
from fastapi.middleware.cors import CORSMiddleware |
|
from fastapi.staticfiles import StaticFiles |
|
|
|
|
|
api_app = FastAPI() |
|
|
|
|
|
api_app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["*"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
|
|
def translate_to_mestreechs(dutch_term): |
|
|
|
if not dutch_term: |
|
return "Error: Empty input" |
|
|
|
if re.search(r'\s{2,}', dutch_term) or len(dutch_term.split()) > 2: |
|
return "Error: Input must be a single word or short term (max 2 words)" |
|
|
|
try: |
|
|
|
encoded_term = urllib.parse.quote(dutch_term) |
|
url = f"https://www.limburgs.net/api/search/{encoded_term}?dialects=Maastricht&languageDirection=NL-LI&deep=false" |
|
|
|
response = requests.get(url, timeout=5) |
|
if response.status_code == 200: |
|
results = response.json() |
|
if results and isinstance(results, list) and results and results[0].get('dialects'): |
|
return results[0]['dialects'][0]['translation'] |
|
return "Translation not found" |
|
except requests.exceptions.Timeout: |
|
return "API timeout - please try again" |
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
@api_app.post("/api/translate") |
|
async def api_translate(request: Request): |
|
data = await request.json() |
|
if not data or 'text' not in data: |
|
raise HTTPException(status_code=400, detail="Missing 'text' in request") |
|
|
|
term = data['text'].strip() |
|
translation = translate_to_mestreechs(term) |
|
|
|
return { |
|
"dutch_term": term, |
|
"mestreechs_translation": translation |
|
} |
|
|
|
|
|
def gradio_translate(dutch_term): |
|
return translate_to_mestreechs(dutch_term) |
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft(), title="Dutch to Mestreechs Translator") as gradio_app: |
|
gr.Markdown("# π³π± Dutch to Mestreechs Translator") |
|
gr.Markdown("Translate Dutch words to the Mestreechs dialect of Limburgish") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
input_text = gr.Textbox( |
|
label="Dutch Word/Term", |
|
placeholder="Enter a Dutch word (e.g. 'huis')", |
|
max_lines=1 |
|
) |
|
submit_btn = gr.Button("Translate", variant="primary") |
|
|
|
gr.Examples( |
|
examples=["huis", "kerk", "oude", "straat", "oude kerk"], |
|
inputs=input_text, |
|
label="Try these examples:" |
|
) |
|
|
|
with gr.Column(): |
|
output_text = gr.Textbox( |
|
label="Mestreechs Translation", |
|
interactive=False |
|
) |
|
|
|
submit_btn.click( |
|
fn=gradio_translate, |
|
inputs=input_text, |
|
outputs=output_text |
|
) |
|
|
|
gr.Markdown(""" |
|
### Notes: |
|
- Input must be a single word or short term (max 2 words) |
|
- Translations provided by [limburgs.net](https://www.limburgs.net) |
|
- Mestreechs is the dialect spoken in Maastricht, Netherlands |
|
- API endpoint: `/api/translate` (POST) |
|
""") |
|
|
|
|
|
api_app.mount("/", gr.routes.App(gradio_app)) |
|
|
|
|
|
if __name__ == "__main__": |
|
uvicorn.run(api_app, host="0.0.0.0", port=7860) |