YOUSEF2434 commited on
Commit
6920945
Β·
verified Β·
1 Parent(s): 316175f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -53
app.py CHANGED
@@ -1,66 +1,40 @@
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)
 
1
+ from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
 
 
 
2
  import gradio as gr
3
 
4
+ # Load M2M100 multilingual model (supports 100+ languages)
5
+ model_name = "facebook/m2m100_418M"
6
+ tokenizer = M2M100Tokenizer.from_pretrained(model_name)
7
+ model = M2M100ForConditionalGeneration.from_pretrained(model_name)
8
+
9
+ def translate_to_english(text):
10
+ # Detect language (roughly by script, can be improved later)
11
+ if any("\u0600" <= c <= "\u06FF" for c in text): # Arabic/Urdu script
12
+ lang = "ar" # works for Urdu too, since M2M100 uses ISO codes
13
+ elif any("\u0900" <= c <= "\u097F" for c in text): # Hindi (Devanagari)
14
+ lang = "hi"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  else:
16
+ lang = "en"
 
17
 
18
+ # If already English, return as is
 
 
19
  if lang == "en":
20
+ return f"πŸ—£ You: {text}\n🌐 Detected: English\nβœ… Translation: {text}"
21
+
22
+ # Set tokenizer to source lang
23
+ tokenizer.src_lang = lang
24
+ encoded = tokenizer(text, return_tensors="pt")
25
+ generated_tokens = model.generate(**encoded, forced_bos_token_id=tokenizer.get_lang_id("en"))
26
+ translation = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
27
+
28
+ return f"πŸ—£ You: {text}\n🌐 Detected: {lang.upper()}\nβœ… Translation: {translation}"
29
 
30
+ # ---- Gradio UI ----
31
  with gr.Blocks(css=".gradio-container {font-family: 'Poppins', sans-serif;}") as demo:
32
+ gr.Markdown("## 🌍 Multilingual β†’ English Chatbot (Arabic, Urdu, Hindi, English)")
 
33
 
34
  with gr.Row():
35
+ inp = gr.Textbox(placeholder="Type something here...", label="Your Message")
36
  out = gr.Textbox(label="Chat Response", interactive=False)
37
 
38
+ inp.submit(translate_to_english, inp, out)
39
 
40
  demo.launch(server_name="0.0.0.0", server_port=7860)