Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# --- 1. Load the two successful models when the app starts ---
|
| 5 |
+
print("Loading Translation model...")
|
| 6 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-swc", device=-1)
|
| 7 |
+
print("✅ Translation model ready.")
|
| 8 |
+
|
| 9 |
+
print("Loading Summarization model...")
|
| 10 |
+
summarizer = pipeline("summarization", model="csebuetnlp/mT5_multilingual_XLSum", device=-1)
|
| 11 |
+
print("✅ Summarization model ready.")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# --- 2. Define the core functions we tested in our notebook ---
|
| 15 |
+
def translate_english_to_swahili(text_to_translate):
|
| 16 |
+
"""Takes English text and returns the Swahili translation."""
|
| 17 |
+
if not text_to_translate:
|
| 18 |
+
return ""
|
| 19 |
+
|
| 20 |
+
# Use the loaded translator model
|
| 21 |
+
result = translator(text_to_translate)
|
| 22 |
+
return result[0]['translation_text']
|
| 23 |
+
|
| 24 |
+
def summarize_kiswahili_text(text_to_summarize):
|
| 25 |
+
"""Takes Kiswahili text and returns a concise summary."""
|
| 26 |
+
if not text_to_summarize:
|
| 27 |
+
return ""
|
| 28 |
+
|
| 29 |
+
# Use the loaded summarizer model
|
| 30 |
+
result = summarizer(text_to_summarize, max_length=60, min_length=15, do_sample=False)
|
| 31 |
+
return result[0]['summary_text']
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# --- 3. Build the Gradio Interface ---
|
| 35 |
+
# We use gr.Blocks() for a custom layout with tabs.
|
| 36 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 37 |
+
gr.Markdown("# Karani v0: AI Language Assistant for Kiswahili 🇰🇪🇹🇿🇺🇬")
|
| 38 |
+
gr.Markdown("An AI assistant for translation and summarization tasks. Select a tab below to begin.")
|
| 39 |
+
|
| 40 |
+
# First Tab for Translation
|
| 41 |
+
with gr.Tab("Tafsiri (Translate)"):
|
| 42 |
+
gr.Markdown("### English to Kiswahili Translation")
|
| 43 |
+
with gr.Row():
|
| 44 |
+
english_input = gr.Textbox(lines=5, label="Enter English Text Here", placeholder="e.g., Artificial intelligence is transforming our world.")
|
| 45 |
+
swahili_output = gr.Textbox(lines=5, label="Swahili Result", interactive=False)
|
| 46 |
+
translate_button = gr.Button("Tafsiri (Translate)")
|
| 47 |
+
|
| 48 |
+
# Second Tab for Summarization
|
| 49 |
+
with gr.Tab("Fupisha (Summarize)"):
|
| 50 |
+
gr.Markdown("### Kiswahili Text Summarization")
|
| 51 |
+
with gr.Row():
|
| 52 |
+
kiswahili_input = gr.Textbox(lines=7, label="Enter Long Kiswahili Text Here")
|
| 53 |
+
summary_output = gr.Textbox(lines=3, label="Summary", interactive=False)
|
| 54 |
+
summarize_button = gr.Button("Fupisha (Summarize)")
|
| 55 |
+
|
| 56 |
+
# --- 4. Define what happens when buttons are clicked ---
|
| 57 |
+
translate_button.click(
|
| 58 |
+
fn=translate_english_to_swahili,
|
| 59 |
+
inputs=english_input,
|
| 60 |
+
outputs=swahili_output
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
summarize_button.click(
|
| 64 |
+
fn=summarize_kiswahili_text,
|
| 65 |
+
inputs=kiswahili_input,
|
| 66 |
+
outputs=summary_output
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# --- 5. Launch the App ---
|
| 70 |
+
demo.launch()
|