Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install gradio transformers pandas torch
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import pandas as pd
|
5 |
+
import torch
|
6 |
+
from transformers import MarianMTModel, MarianTokenizer
|
7 |
+
|
8 |
+
# Load the fine-tuned MarianMT model and tokenizer
|
9 |
+
# Replace with the path to your model directory
|
10 |
+
model_dir = '/content/drive/MyDrive/fine_tuned_marian' # Replace with the correct path
|
11 |
+
model = MarianMTModel.from_pretrained(model_dir)
|
12 |
+
tokenizer = MarianTokenizer.from_pretrained(model_dir)
|
13 |
+
|
14 |
+
# Function to translate text
|
15 |
+
def translate_arabic_to_english(arabic_text):
|
16 |
+
# Tokenize the input text
|
17 |
+
inputs = tokenizer(arabic_text, return_tensors="pt", padding=True, truncation=True, max_length=128)
|
18 |
+
|
19 |
+
# Move inputs to the same device as the model
|
20 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
21 |
+
model.to(device)
|
22 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
23 |
+
|
24 |
+
# Generate translation
|
25 |
+
with torch.no_grad():
|
26 |
+
translated_ids = model.generate(**inputs)
|
27 |
+
|
28 |
+
# Decode the translated text
|
29 |
+
translated_text = tokenizer.decode(translated_ids[0], skip_special_tokens=True)
|
30 |
+
return translated_text
|
31 |
+
|
32 |
+
# Create the Gradio interface
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=translate_arabic_to_english,
|
35 |
+
inputs=gr.Textbox(lines=5, placeholder="Enter Arabic text here..."),
|
36 |
+
outputs="text",
|
37 |
+
title="Arabic to English Machine Translation",
|
38 |
+
description="Translate Arabic text to English ",
|
39 |
+
)
|
40 |
+
|
41 |
+
# Launch the interface
|
42 |
+
iface.launch()
|