Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
|
| 4 |
+
try:
|
| 5 |
+
model_name = "ai4bharat/IndicTrans3-beta"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True, use_fast=False)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
| 8 |
+
except Exception as e:
|
| 9 |
+
raise Exception(f"Error loading model: {str(e)}")
|
| 10 |
+
|
| 11 |
+
def translate(text, src_lang, tgt_lang):
|
| 12 |
+
if not text:
|
| 13 |
+
return "Please enter text to translate."
|
| 14 |
+
try:
|
| 15 |
+
prompt = f"Translate from {src_lang} to {tgt_lang}: {text}"
|
| 16 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True)
|
| 17 |
+
outputs = model.generate(**inputs, max_new_tokens=50)
|
| 18 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"Translation error: {str(e)}"
|
| 21 |
+
|
| 22 |
+
lang_options = ["en", "hi", "ta", "te"]
|
| 23 |
+
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=translate,
|
| 26 |
+
inputs=[
|
| 27 |
+
gr.Textbox(label="Input Text", placeholder="Enter text here..."),
|
| 28 |
+
gr.Dropdown(lang_options, label="Source Language", value="en"),
|
| 29 |
+
gr.Dropdown(lang_options, label="Target Language", value="hi")
|
| 30 |
+
],
|
| 31 |
+
outputs=gr.Textbox(label="Translation"),
|
| 32 |
+
title="Indic Translator",
|
| 33 |
+
description="Translate text using AI4Bharat's IndicTrans3-beta model."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|