Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from transformers import MarianMTModel, MarianTokenizer, pipeline
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load translation models (xx -> en)
|
| 8 |
+
models = {
|
| 9 |
+
"ar": pipeline("translation", model="Helsinki-NLP/opus-mt-ar-en"),
|
| 10 |
+
"ur": pipeline("translation", model="Helsinki-NLP/opus-mt-ur-en"),
|
| 11 |
+
"hi": pipeline("translation", model="Helsinki-NLP/opus-mt-hi-en"),
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
# Dummy fastapi backend
|
| 15 |
+
app = FastAPI()
|
| 16 |
+
app.add_middleware(
|
| 17 |
+
CORSMiddleware,
|
| 18 |
+
allow_origins=["*"],
|
| 19 |
+
allow_credentials=True,
|
| 20 |
+
allow_methods=["*"],
|
| 21 |
+
allow_headers=["*"],
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
class TranslationRequest(BaseModel):
|
| 25 |
+
text: str
|
| 26 |
+
|
| 27 |
+
def detect_language(text: str) -> str:
|
| 28 |
+
"""Very basic language detector based on script/characters."""
|
| 29 |
+
if any("\u0600" <= c <= "\u06FF" for c in text): # Arabic/Urdu
|
| 30 |
+
# Urdu detection heuristic
|
| 31 |
+
if "ے" in text or "ں" in text:
|
| 32 |
+
return "ur"
|
| 33 |
+
return "ar"
|
| 34 |
+
elif any("\u0900" <= c <= "\u097F" for c in text): # Devanagari -> Hindi
|
| 35 |
+
return "hi"
|
| 36 |
+
return "en"
|
| 37 |
+
|
| 38 |
+
@app.post("/translate")
|
| 39 |
+
def translate(req: TranslationRequest):
|
| 40 |
+
lang = detect_language(req.text)
|
| 41 |
+
if lang == "en":
|
| 42 |
+
return {"input": req.text, "translation": req.text, "lang": "en"}
|
| 43 |
+
else:
|
| 44 |
+
translated = models[lang](req.text)[0]["translation_text"]
|
| 45 |
+
return {"input": req.text, "translation": translated, "lang": lang}
|
| 46 |
+
|
| 47 |
+
# ---- Gradio Interface ----
|
| 48 |
+
def chat_interface(user_input):
|
| 49 |
+
lang = detect_language(user_input)
|
| 50 |
+
if lang == "en":
|
| 51 |
+
return f"🗣 You: {user_input}\n\n🌐 Detected: English\n\n✅ Translation: {user_input}"
|
| 52 |
+
else:
|
| 53 |
+
translated = models[lang](user_input)[0]["translation_text"]
|
| 54 |
+
return f"🗣 You: {user_input}\n\n🌐 Detected: {lang.upper()}\n\n✅ Translation: {translated}"
|
| 55 |
+
|
| 56 |
+
with gr.Blocks(css=".gradio-container {font-family: 'Poppins', sans-serif;}") as demo:
|
| 57 |
+
gr.Markdown("## 🌍 Multilingual → English Chatbot")
|
| 58 |
+
gr.Markdown("Type anything in **Arabic, Urdu, Hindi, or English**, and I’ll always answer in English!")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
inp = gr.Textbox(placeholder="Type here...", label="Your Message")
|
| 62 |
+
out = gr.Textbox(label="Chat Response", interactive=False)
|
| 63 |
+
|
| 64 |
+
inp.submit(chat_interface, inp, out)
|
| 65 |
+
|
| 66 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|