Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
from huggingface_hub import login
|
| 5 |
+
|
| 6 |
+
# Placeholder for the model pipeline
|
| 7 |
+
pipe = None
|
| 8 |
+
|
| 9 |
+
def validate_and_load(token):
|
| 10 |
+
"""Handles the 'login' logic and loads the model."""
|
| 11 |
+
global pipe
|
| 12 |
+
try:
|
| 13 |
+
# Authenticate
|
| 14 |
+
login(token=token)
|
| 15 |
+
|
| 16 |
+
# Load the model (Moving this here ensures it only loads after login)
|
| 17 |
+
# Using bfloat16 and device_map for professional performance
|
| 18 |
+
pipe = pipeline(
|
| 19 |
+
"text-generation",
|
| 20 |
+
model="ibm-granite/granite-3.3-2b-base",
|
| 21 |
+
torch_dtype=torch.bfloat16,
|
| 22 |
+
device_map="auto"
|
| 23 |
+
)
|
| 24 |
+
# Return a success message and toggle UI visibility
|
| 25 |
+
return gr.update(visible=False), gr.update(visible=True), "✅ Authentication Successful! Model Loaded."
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return gr.update(visible=True), gr.update(visible=False), f"❌ Error: {str(e)}"
|
| 28 |
+
|
| 29 |
+
def translate_text(text, target_language):
|
| 30 |
+
if pipe is None:
|
| 31 |
+
return "Please login first."
|
| 32 |
+
|
| 33 |
+
# Prompt engineering for the Base model
|
| 34 |
+
prompt = f"Translate the following English text to {target_language}:\nEnglish: {text}\n{target_language}:"
|
| 35 |
+
|
| 36 |
+
outputs = pipe(
|
| 37 |
+
prompt,
|
| 38 |
+
max_new_tokens=150,
|
| 39 |
+
do_sample=False,
|
| 40 |
+
return_full_text=False
|
| 41 |
+
)
|
| 42 |
+
return outputs[0]['generated_text'].strip()
|
| 43 |
+
|
| 44 |
+
# --- UI DESIGN ---
|
| 45 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Granite Translator Pro") as demo:
|
| 46 |
+
|
| 47 |
+
# Header Section
|
| 48 |
+
gr.Markdown("""
|
| 49 |
+
# 🌐 Granite Multi-Lingual Pro
|
| 50 |
+
### Enterprise-grade translation powered by IBM Granite 3.3 2B
|
| 51 |
+
""")
|
| 52 |
+
|
| 53 |
+
# 1. AUTHENTICATION SECTION (Visible by default)
|
| 54 |
+
with gr.Column(visible=True) as auth_section:
|
| 55 |
+
gr.Markdown("### 🔐 Authentication Required")
|
| 56 |
+
hf_token = gr.Textbox(
|
| 57 |
+
label="Hugging Face Access Token",
|
| 58 |
+
placeholder="hf_...",
|
| 59 |
+
type="password",
|
| 60 |
+
info="Enter your read-access token to begin."
|
| 61 |
+
)
|
| 62 |
+
login_btn = gr.Button("Initialize Application", variant="primary")
|
| 63 |
+
status_msg = gr.Markdown()
|
| 64 |
+
|
| 65 |
+
# 2. MAIN APPLICATION SECTION (Hidden by default)
|
| 66 |
+
with gr.Column(visible=False) as main_app:
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
input_text = gr.Textbox(
|
| 70 |
+
label="Input Text (English)",
|
| 71 |
+
placeholder="Type something here...",
|
| 72 |
+
lines=5
|
| 73 |
+
)
|
| 74 |
+
target_lang = gr.Dropdown(
|
| 75 |
+
label="Target Language",
|
| 76 |
+
choices=["Hindi (हिन्दी)", "Gujarati (ગુજરાતી)", "Spanish", "French", "German"],
|
| 77 |
+
value="Hindi (हिन्दी)"
|
| 78 |
+
)
|
| 79 |
+
translate_btn = gr.Button("Translate Now", variant="primary")
|
| 80 |
+
|
| 81 |
+
with gr.Column():
|
| 82 |
+
output_text = gr.Textbox(label="Translated Result", lines=8, interactive=False)
|
| 83 |
+
|
| 84 |
+
gr.ClearButton([input_text, output_text])
|
| 85 |
+
|
| 86 |
+
# --- LOGIC FLOW ---
|
| 87 |
+
# When login is clicked, validate token and switch views
|
| 88 |
+
login_btn.click(
|
| 89 |
+
fn=validate_and_load,
|
| 90 |
+
inputs=[hf_token],
|
| 91 |
+
outputs=[auth_section, main_app, status_msg]
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Translation trigger
|
| 95 |
+
translate_btn.click(
|
| 96 |
+
fn=translate_text,
|
| 97 |
+
inputs=[input_text, target_lang],
|
| 98 |
+
outputs=[output_text]
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|