Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
-
import torch
|
4 |
-
import threading
|
5 |
-
from huggingface_hub import hf_hub_download, login
|
6 |
import sys
|
7 |
-
import
|
|
|
|
|
8 |
|
9 |
login(token=os.environ["HF_TOKEN"])
|
10 |
tokenizer = os.environ["TOKENIZER"]
|
11 |
repo_id = os.environ["REPO_ID"]
|
12 |
|
|
|
13 |
torch.set_num_threads(1)
|
14 |
if torch.cuda.is_available():
|
15 |
torch.backends.cudnn.benchmark = True
|
@@ -17,64 +17,194 @@ if torch.cuda.is_available():
|
|
17 |
try:
|
18 |
generate_file = hf_hub_download(
|
19 |
repo_id=repo_id,
|
20 |
-
filename="
|
21 |
token=os.environ["HF_TOKEN"]
|
22 |
)
|
23 |
-
os.system(f"cp {generate_file} ./
|
24 |
-
|
25 |
except Exception as e:
|
26 |
print(f"Error downloading files: {e}")
|
27 |
-
model_path = None
|
28 |
|
29 |
sys.path.append('.')
|
30 |
-
from
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
max-width: 900px;
|
59 |
-
margin: 0 auto;
|
60 |
-
padding: 20px;
|
61 |
-
}
|
62 |
-
.center-text {
|
63 |
-
text-align: center;
|
64 |
-
margin-bottom: 30px;
|
65 |
}
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
background-color: #f8f9fa;
|
73 |
-
border-radius: 8px;
|
74 |
-
border-left: 4px solid #007bff;
|
75 |
}
|
76 |
"""
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
if __name__ == "__main__":
|
80 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
|
|
|
|
3 |
import sys
|
4 |
+
import base64
|
5 |
+
import threading
|
6 |
+
from huggingface_hub import login, hf_hub_download
|
7 |
|
8 |
login(token=os.environ["HF_TOKEN"])
|
9 |
tokenizer = os.environ["TOKENIZER"]
|
10 |
repo_id = os.environ["REPO_ID"]
|
11 |
|
12 |
+
import torch
|
13 |
torch.set_num_threads(1)
|
14 |
if torch.cuda.is_available():
|
15 |
torch.backends.cudnn.benchmark = True
|
|
|
17 |
try:
|
18 |
generate_file = hf_hub_download(
|
19 |
repo_id=repo_id,
|
20 |
+
filename="generate.py",
|
21 |
token=os.environ["HF_TOKEN"]
|
22 |
)
|
23 |
+
os.system(f"cp {generate_file} ./generate.py")
|
|
|
24 |
except Exception as e:
|
25 |
print(f"Error downloading files: {e}")
|
|
|
26 |
|
27 |
sys.path.append('.')
|
28 |
+
from generate import chat_interface, init_model
|
29 |
|
30 |
+
LOGO_PATH = "static/logo.png"
|
31 |
+
if os.path.isfile(LOGO_PATH):
|
32 |
+
with open(LOGO_PATH, "rb") as f:
|
33 |
+
LOGO_B64 = base64.b64encode(f.read()).decode()
|
34 |
+
LOGO_HTML = f'<img src="data:image/png;base64,{LOGO_B64}" alt="Maruth Labs Logo" style="height:40px;">'
|
35 |
+
FAVICON_HTML = f'<link rel="icon" type="image/png" href="data:image/png;base64,{LOGO_B64}">'
|
36 |
+
else:
|
37 |
+
LOGO_HTML = '<div style="width:40px;height:40px;background:#ccc;border-radius:4px;"></div>'
|
38 |
+
FAVICON_HTML = ''
|
39 |
|
40 |
+
def init_translation_model():
|
41 |
+
success = init_model(tokenizer_path=tokenizer)
|
42 |
+
if success:
|
43 |
+
print("Model loaded successfully!")
|
44 |
+
else:
|
45 |
+
print("Failed to load model")
|
46 |
+
|
47 |
+
def translate_text(source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens):
|
48 |
+
return chat_interface(
|
49 |
+
source_text, source_lang, target_lang,
|
50 |
+
temperature, top_k, repetition_penalty, max_tokens
|
51 |
+
)
|
52 |
+
|
53 |
+
languages = ["English", "Hindi", "Bengali", "Tamil", "Telugu", "Kannada", "Panjabi"]
|
54 |
+
|
55 |
+
css_path = "static/style.css"
|
56 |
+
custom_css = open(css_path, encoding="utf-8").read() if os.path.isfile(css_path) else ""
|
57 |
+
|
58 |
+
theme_lock_css = """
|
59 |
+
.gradio-container .theme-toggle,
|
60 |
+
.gradio-container button[aria-label*="theme"],
|
61 |
+
.gradio-container button[title*="theme"],
|
62 |
+
.gradio-container .settings button,
|
63 |
+
.gradio-container [data-testid="theme-toggle"] {
|
64 |
+
display: none !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
}
|
66 |
+
|
67 |
+
:root { color-scheme: dark !important; }
|
68 |
+
|
69 |
+
body, .gradio-container {
|
70 |
+
background-color: #0a1628 !important;
|
71 |
+
color: #e6eef8 !important;
|
|
|
|
|
|
|
72 |
}
|
73 |
"""
|
74 |
|
75 |
+
combined_css = custom_css + theme_lock_css
|
76 |
+
|
77 |
+
locked_theme = gr.themes.Monochrome(
|
78 |
+
primary_hue="blue",
|
79 |
+
secondary_hue="slate",
|
80 |
+
neutral_hue="slate"
|
81 |
+
).set(
|
82 |
+
background_fill_primary="#0a1628",
|
83 |
+
background_fill_secondary="#1f2937",
|
84 |
+
block_background_fill="#374151",
|
85 |
+
border_color_primary="#374151",
|
86 |
+
color_accent_soft="#2563eb",
|
87 |
+
block_title_text_color="#e6eef8",
|
88 |
+
block_label_text_color="#e6eef8",
|
89 |
+
body_text_color="#e6eef8"
|
90 |
+
)
|
91 |
+
|
92 |
+
with gr.Blocks(
|
93 |
+
title="Madhuram Translation - MaruthLabs",
|
94 |
+
css=combined_css,
|
95 |
+
theme=locked_theme,
|
96 |
+
js=f"""
|
97 |
+
function() {{
|
98 |
+
{f'document.head.insertAdjacentHTML("beforeend", `{FAVICON_HTML}`);' if FAVICON_HTML else ''}
|
99 |
+
|
100 |
+
document.documentElement.setAttribute('data-theme', 'dark');
|
101 |
+
document.body.classList.add('dark');
|
102 |
+
document.body.classList.remove('light');
|
103 |
+
|
104 |
+
const observer = new MutationObserver(function(mutations) {{
|
105 |
+
mutations.forEach(function(mutation) {{
|
106 |
+
mutation.addedNodes.forEach(function(node) {{
|
107 |
+
if (node.nodeType === 1) {{
|
108 |
+
const toggles = node.querySelectorAll('.theme-toggle, button[aria-label*="theme"], button[title*="theme"]');
|
109 |
+
toggles.forEach(toggle => toggle.style.display = 'none');
|
110 |
+
}}
|
111 |
+
}});
|
112 |
+
}});
|
113 |
+
}});
|
114 |
+
observer.observe(document.body, {{ childList: true, subtree: true }});
|
115 |
+
}}
|
116 |
+
"""
|
117 |
+
) as demo:
|
118 |
+
|
119 |
+
with gr.Row(elem_classes="main-header"):
|
120 |
+
with gr.Column():
|
121 |
+
gr.HTML(f"""
|
122 |
+
<div style="display:flex;align-items:center;justify-content:space-between;width:100%;">
|
123 |
+
<!-- left: logo + text on one line -->
|
124 |
+
<div style="display:flex;align-items:center;">
|
125 |
+
{LOGO_HTML}
|
126 |
+
<h3 style="margin-left:8px;margin-top:0;margin-bottom:0;">Maruth Labs</h3>
|
127 |
+
</div>
|
128 |
+
|
129 |
+
<!-- center title -->
|
130 |
+
<div class="main-title"><h1>Madhuram Translation Model</h1></div>
|
131 |
+
|
132 |
+
<!-- spacer to balance flex -->
|
133 |
+
<div style="width:120px;"></div>
|
134 |
+
</div>
|
135 |
+
""")
|
136 |
+
|
137 |
+
with gr.Row(equal_height=False):
|
138 |
+
with gr.Column(scale=1.5, elem_classes="settings-panel"):
|
139 |
+
gr.Markdown("## Translation Settings")
|
140 |
+
with gr.Row():
|
141 |
+
source_lang = gr.Dropdown(choices=languages, label="Source Language", value="English")
|
142 |
+
target_lang = gr.Dropdown(choices=languages, label="Target Language", value="Hindi")
|
143 |
+
swap_btn = gr.Button("Swap Languages", variant="secondary", size="sm")
|
144 |
+
|
145 |
+
with gr.Accordion("Advanced Settings", open=False):
|
146 |
+
temperature = gr.Slider(0.001, 1.001, 0.001, step=0.1, label="Temperature")
|
147 |
+
top_k = gr.Slider(1, 100, 10, step=1, label="Top-k")
|
148 |
+
repetition_penalty = gr.Slider(1.0, 2.0, 1.2, step=0.1, label="Repetition Penalty")
|
149 |
+
max_tokens = gr.Slider(100, 2000, 400, step=50, label="Max Tokens")
|
150 |
+
|
151 |
+
with gr.Column(scale=2, elem_classes="translation-card"):
|
152 |
+
gr.Markdown("## Translation Interface")
|
153 |
+
source_text = gr.Textbox(
|
154 |
+
label="Enter text to translate",
|
155 |
+
placeholder="Type or paste your text here",
|
156 |
+
lines=6,
|
157 |
+
max_lines=12
|
158 |
+
)
|
159 |
+
with gr.Row():
|
160 |
+
translate_btn = gr.Button("Translate", variant="primary", size="lg")
|
161 |
+
clear_btn = gr.Button("Clear All", variant="secondary", size="lg")
|
162 |
+
translated_text = gr.Textbox(
|
163 |
+
label="Translation Result",
|
164 |
+
lines=6,
|
165 |
+
max_lines=12,
|
166 |
+
interactive=False,
|
167 |
+
placeholder="Translation will appear here"
|
168 |
+
)
|
169 |
+
|
170 |
+
with gr.Row():
|
171 |
+
with gr.Column():
|
172 |
+
gr.Markdown("### Quick Examples")
|
173 |
+
gr.Examples(
|
174 |
+
examples=[
|
175 |
+
["Hello, how are you today?", "English", "Hindi"],
|
176 |
+
["তুমি কোথায় যাচ্ছ?", "Bengali", "English"],
|
177 |
+
["நீங்கள் எப்படி இருக்கிறீர்கள்?", "Tamil", "Telugu"],
|
178 |
+
["ನಿನ್ನ ಹೆಸರು ಏನು?", "Kannada", "English"],
|
179 |
+
["ਸਤ ਸ੍ਰੀ ਅਕਾਲ", "Panjabi", "Hindi"],
|
180 |
+
],
|
181 |
+
inputs=[source_text, source_lang, target_lang],
|
182 |
+
)
|
183 |
+
|
184 |
+
def swap_languages(src, tgt):
|
185 |
+
return tgt, src
|
186 |
+
|
187 |
+
def clear_all():
|
188 |
+
return "", ""
|
189 |
+
|
190 |
+
swap_btn.click(
|
191 |
+
fn=swap_languages,
|
192 |
+
inputs=[source_lang, target_lang],
|
193 |
+
outputs=[source_lang, target_lang]
|
194 |
+
)
|
195 |
+
|
196 |
+
clear_btn.click(
|
197 |
+
fn=clear_all,
|
198 |
+
outputs=[source_text, translated_text]
|
199 |
+
)
|
200 |
+
|
201 |
+
translate_btn.click(
|
202 |
+
fn=translate_text,
|
203 |
+
inputs=[source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens],
|
204 |
+
outputs=[translated_text]
|
205 |
+
)
|
206 |
+
|
207 |
+
demo.load(fn=init_translation_model)
|
208 |
|
209 |
if __name__ == "__main__":
|
210 |
demo.launch()
|