Spaces:
Sleeping
Sleeping
app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,181 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
| 42 |
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
"""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# ----------------------------------------
|
| 6 |
+
# Supported Languages
|
| 7 |
+
# ----------------------------------------
|
| 8 |
+
languages = [
|
| 9 |
+
"English",
|
| 10 |
+
"Hindi",
|
| 11 |
+
"Tamil",
|
| 12 |
+
"Telugu",
|
| 13 |
+
"Kannada",
|
| 14 |
+
"Malayalam",
|
| 15 |
+
"Bengali",
|
| 16 |
+
"Marathi",
|
| 17 |
+
"Gujarati",
|
| 18 |
+
"Punjabi",
|
| 19 |
+
"Urdu"
|
| 20 |
+
]
|
| 21 |
|
| 22 |
+
# ----------------------------------------
|
| 23 |
+
# Translation Function
|
| 24 |
+
# ----------------------------------------
|
| 25 |
+
def translate_text(hf_token, source_lang, target_lang, input_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
if not hf_token.strip():
|
| 28 |
+
return "Please enter Hugging Face Access Token."
|
| 29 |
|
| 30 |
+
if not input_text.strip():
|
| 31 |
+
return "Please enter text."
|
| 32 |
|
| 33 |
+
try:
|
| 34 |
|
| 35 |
+
# Load IBM Granite model
|
| 36 |
+
pipe = pipeline(
|
| 37 |
+
"text-generation",
|
| 38 |
+
model="ibm-granite/granite-3.3-2b-base",
|
| 39 |
+
token=hf_token,
|
| 40 |
+
device_map="auto",
|
| 41 |
+
torch_dtype=torch.float16
|
| 42 |
+
)
|
| 43 |
|
| 44 |
+
# Translation Prompt
|
| 45 |
+
prompt = f"""
|
| 46 |
+
You are an expert multilingual translator.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
Translate the below text from {source_lang} to {target_lang}.
|
|
|
|
| 49 |
|
| 50 |
+
Only provide translated text.
|
| 51 |
|
| 52 |
+
Text:
|
| 53 |
+
{input_text}
|
| 54 |
"""
|
| 55 |
+
|
| 56 |
+
# Generate Translation
|
| 57 |
+
result = pipe(
|
| 58 |
+
prompt,
|
| 59 |
+
max_new_tokens=200,
|
| 60 |
+
temperature=0.2
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
translated_text = result[0]["generated_text"]
|
| 64 |
+
|
| 65 |
+
# Remove original prompt if generated
|
| 66 |
+
translated_text = translated_text.replace(prompt, "").strip()
|
| 67 |
+
|
| 68 |
+
return translated_text
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return f"Error: {str(e)}"
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# ----------------------------------------
|
| 75 |
+
# Professional UI Styling
|
| 76 |
+
# ----------------------------------------
|
| 77 |
+
custom_css = """
|
| 78 |
+
body {
|
| 79 |
+
background: linear-gradient(to right, #0f2027, #203a43, #2c5364);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
.gradio-container {
|
| 83 |
+
font-family: 'Segoe UI', sans-serif;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
.main-title {
|
| 87 |
+
text-align: center;
|
| 88 |
+
font-size: 42px;
|
| 89 |
+
font-weight: bold;
|
| 90 |
+
color: white;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.sub-title {
|
| 94 |
+
text-align: center;
|
| 95 |
+
color: #dfe6e9;
|
| 96 |
+
font-size: 18px;
|
| 97 |
+
margin-bottom: 20px;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
textarea {
|
| 101 |
+
border-radius: 12px !important;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
footer {
|
| 105 |
+
visibility: hidden;
|
| 106 |
+
}
|
| 107 |
"""
|
| 108 |
+
|
| 109 |
+
# ----------------------------------------
|
| 110 |
+
# Gradio Interface
|
| 111 |
+
# ----------------------------------------
|
| 112 |
+
with gr.Blocks(
|
| 113 |
+
theme=gr.themes.Soft(),
|
| 114 |
+
css=custom_css
|
| 115 |
+
) as demo:
|
| 116 |
+
|
| 117 |
+
gr.Markdown(
|
| 118 |
+
"""
|
| 119 |
+
<div class="main-title">
|
| 120 |
+
π AI Multilingual Translator
|
| 121 |
+
</div>
|
| 122 |
+
|
| 123 |
+
<div class="sub-title">
|
| 124 |
+
IBM Granite + Hugging Face + Gradio
|
| 125 |
+
</div>
|
| 126 |
+
"""
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
with gr.Row():
|
| 130 |
+
|
| 131 |
+
with gr.Column(scale=1):
|
| 132 |
+
|
| 133 |
+
hf_token = gr.Textbox(
|
| 134 |
+
label="π Hugging Face Access Token",
|
| 135 |
+
placeholder="Paste your HF token here",
|
| 136 |
+
type="password"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
source_lang = gr.Dropdown(
|
| 140 |
+
choices=languages,
|
| 141 |
+
value="English",
|
| 142 |
+
label="π Source Language"
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
target_lang = gr.Dropdown(
|
| 146 |
+
choices=languages,
|
| 147 |
+
value="Hindi",
|
| 148 |
+
label="π― Target Language"
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
with gr.Column(scale=2):
|
| 152 |
+
|
| 153 |
+
input_text = gr.Textbox(
|
| 154 |
+
label="π Enter Text",
|
| 155 |
+
lines=8,
|
| 156 |
+
placeholder="Type text to translate..."
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
output_text = gr.Textbox(
|
| 160 |
+
label="β
Translated Output",
|
| 161 |
+
lines=8
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
translate_btn = gr.Button(
|
| 165 |
+
"π Translate",
|
| 166 |
+
variant="primary"
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
translate_btn.click(
|
| 170 |
+
fn=translate_text,
|
| 171 |
+
inputs=[
|
| 172 |
+
hf_token,
|
| 173 |
+
source_lang,
|
| 174 |
+
target_lang,
|
| 175 |
+
input_text
|
| 176 |
+
],
|
| 177 |
+
outputs=output_text
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# Launch App
|
| 181 |
+
demo.launch(share=True)
|