Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,31 +5,23 @@ from huggingface_hub import hf_hub_download, snapshot_download, login
|
|
5 |
import sys
|
6 |
import gc
|
7 |
import base64
|
|
|
8 |
|
9 |
login(token=os.environ["HF_TOKEN"])
|
10 |
repo_id = os.environ["REPO_ID"]
|
11 |
|
12 |
-
# Torch optimizations
|
13 |
-
import torch
|
14 |
torch.set_num_threads(1)
|
15 |
if torch.cuda.is_available():
|
16 |
torch.backends.cudnn.benchmark = True
|
17 |
|
18 |
-
# Download generation logic from private repo
|
19 |
try:
|
20 |
-
generate_file = hf_hub_download(
|
21 |
-
repo_id=repo_id,
|
22 |
-
filename="gen1.py",
|
23 |
-
token=os.environ["HF_TOKEN"]
|
24 |
-
)
|
25 |
os.system(f"cp {generate_file} ./gen1.py")
|
26 |
except Exception as e:
|
27 |
print(f"Error downloading files: {e}")
|
28 |
|
29 |
-
# Import the generation wrapper
|
30 |
from gen1 import setup_translation, translate_text as gen_translate
|
31 |
|
32 |
-
# Logo and favicon setup
|
33 |
LOGO_PATH = "static/logo.png"
|
34 |
if os.path.isfile(LOGO_PATH):
|
35 |
with open(LOGO_PATH, "rb") as f:
|
@@ -41,29 +33,18 @@ else:
|
|
41 |
FAVICON_HTML = ''
|
42 |
|
43 |
def init_translation_model():
|
44 |
-
""
|
45 |
-
success = setup_translation(
|
46 |
-
repo_id=os.environ["REPO_ID"],
|
47 |
-
token=os.environ["HF_TOKEN"]
|
48 |
-
)
|
49 |
if success:
|
50 |
print("Model loaded successfully!")
|
51 |
else:
|
52 |
print("Failed to load model")
|
53 |
|
54 |
def translate_text(source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens):
|
55 |
-
|
56 |
-
return gen_translate(
|
57 |
-
source_text, source_lang, target_lang,
|
58 |
-
temperature, top_k, repetition_penalty, max_tokens
|
59 |
-
)
|
60 |
|
61 |
-
# Language options
|
62 |
languages = ["English", "Hindi", "Bengali", "Tamil", "Telugu", "Kannada", "Panjabi"]
|
63 |
-
|
64 |
-
#
|
65 |
-
css_path = "static/style.css"
|
66 |
-
custom_css = open(css_path, encoding="utf-8").read() if os.path.isfile(css_path) else ""
|
67 |
|
68 |
theme_lock_css = """
|
69 |
.gradio-container .theme-toggle,
|
@@ -82,14 +63,8 @@ body, .gradio-container {
|
|
82 |
}
|
83 |
"""
|
84 |
|
85 |
-
combined_css =
|
86 |
-
|
87 |
-
# Theme configuration
|
88 |
-
locked_theme = gr.themes.Monochrome(
|
89 |
-
primary_hue="blue",
|
90 |
-
secondary_hue="slate",
|
91 |
-
neutral_hue="slate"
|
92 |
-
).set(
|
93 |
background_fill_primary="#0a1628",
|
94 |
background_fill_secondary="#1f2937",
|
95 |
block_background_fill="#374151",
|
@@ -100,10 +75,9 @@ locked_theme = gr.themes.Monochrome(
|
|
100 |
body_text_color="#e6eef8"
|
101 |
)
|
102 |
|
103 |
-
# Create Gradio interface
|
104 |
with gr.Blocks(
|
105 |
title="Madhuram Translation - MaruthLabs",
|
106 |
-
css=
|
107 |
theme=locked_theme,
|
108 |
js=f"""
|
109 |
function() {{
|
@@ -128,7 +102,6 @@ with gr.Blocks(
|
|
128 |
"""
|
129 |
) as demo:
|
130 |
|
131 |
-
# Header section
|
132 |
with gr.Row(elem_classes="main-header"):
|
133 |
with gr.Column():
|
134 |
gr.HTML(f"""
|
@@ -147,9 +120,7 @@ with gr.Blocks(
|
|
147 |
</div>
|
148 |
""")
|
149 |
|
150 |
-
# Main interface
|
151 |
with gr.Row(equal_height=False):
|
152 |
-
# Settings panel
|
153 |
with gr.Column(scale=1.5, elem_classes="settings-panel"):
|
154 |
gr.Markdown("## Translation Settings")
|
155 |
with gr.Row():
|
@@ -163,7 +134,6 @@ with gr.Blocks(
|
|
163 |
repetition_penalty = gr.Slider(1.0, 2.0, 1.2, step=0.1, label="Repetition Penalty")
|
164 |
max_tokens = gr.Slider(100, 2000, 400, step=50, label="Max Tokens")
|
165 |
|
166 |
-
# Translation interface
|
167 |
with gr.Column(scale=2, elem_classes="translation-card"):
|
168 |
gr.Markdown("## Translation Interface")
|
169 |
source_text = gr.Textbox(
|
@@ -183,7 +153,6 @@ with gr.Blocks(
|
|
183 |
placeholder="Translation will appear here"
|
184 |
)
|
185 |
|
186 |
-
# Examples section
|
187 |
with gr.Row():
|
188 |
with gr.Column():
|
189 |
gr.Markdown("### Quick Examples")
|
@@ -198,32 +167,15 @@ with gr.Blocks(
|
|
198 |
inputs=[source_text, source_lang, target_lang],
|
199 |
)
|
200 |
|
201 |
-
# Event handlers
|
202 |
def swap_languages(src, tgt):
|
203 |
return tgt, src
|
204 |
|
205 |
def clear_all():
|
206 |
return "", ""
|
207 |
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
inputs=[source_lang, target_lang],
|
212 |
-
outputs=[source_lang, target_lang]
|
213 |
-
)
|
214 |
-
|
215 |
-
clear_btn.click(
|
216 |
-
fn=clear_all,
|
217 |
-
outputs=[source_text, translated_text]
|
218 |
-
)
|
219 |
-
|
220 |
-
translate_btn.click(
|
221 |
-
fn=translate_text,
|
222 |
-
inputs=[source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens],
|
223 |
-
outputs=[translated_text]
|
224 |
-
)
|
225 |
-
|
226 |
-
# Load model when demo starts
|
227 |
demo.load(fn=init_translation_model)
|
228 |
|
229 |
if __name__ == "__main__":
|
|
|
5 |
import sys
|
6 |
import gc
|
7 |
import base64
|
8 |
+
import torch
|
9 |
|
10 |
login(token=os.environ["HF_TOKEN"])
|
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
|
16 |
|
|
|
17 |
try:
|
18 |
+
generate_file = hf_hub_download(repo_id=repo_id, filename="gen1.py", token=os.environ["HF_TOKEN"])
|
|
|
|
|
|
|
|
|
19 |
os.system(f"cp {generate_file} ./gen1.py")
|
20 |
except Exception as e:
|
21 |
print(f"Error downloading files: {e}")
|
22 |
|
|
|
23 |
from gen1 import setup_translation, translate_text as gen_translate
|
24 |
|
|
|
25 |
LOGO_PATH = "static/logo.png"
|
26 |
if os.path.isfile(LOGO_PATH):
|
27 |
with open(LOGO_PATH, "rb") as f:
|
|
|
33 |
FAVICON_HTML = ''
|
34 |
|
35 |
def init_translation_model():
|
36 |
+
success = setup_translation(repo_id=os.environ["REPO_ID"], token=os.environ["HF_TOKEN"])
|
|
|
|
|
|
|
|
|
37 |
if success:
|
38 |
print("Model loaded successfully!")
|
39 |
else:
|
40 |
print("Failed to load model")
|
41 |
|
42 |
def translate_text(source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens):
|
43 |
+
return gen_translate(source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens)
|
|
|
|
|
|
|
|
|
44 |
|
|
|
45 |
languages = ["English", "Hindi", "Bengali", "Tamil", "Telugu", "Kannada", "Panjabi"]
|
46 |
+
# css_path = "static/style.css"
|
47 |
+
# custom_css = open(css_path, encoding="utf-8").read() if os.path.isfile(css_path) else ""
|
|
|
|
|
48 |
|
49 |
theme_lock_css = """
|
50 |
.gradio-container .theme-toggle,
|
|
|
63 |
}
|
64 |
"""
|
65 |
|
66 |
+
# combined_css = theme_lock_css
|
67 |
+
locked_theme = gr.themes.Monochrome(primary_hue="blue", secondary_hue="slate", neutral_hue="slate").set(
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
background_fill_primary="#0a1628",
|
69 |
background_fill_secondary="#1f2937",
|
70 |
block_background_fill="#374151",
|
|
|
75 |
body_text_color="#e6eef8"
|
76 |
)
|
77 |
|
|
|
78 |
with gr.Blocks(
|
79 |
title="Madhuram Translation - MaruthLabs",
|
80 |
+
css=theme_lock_css,
|
81 |
theme=locked_theme,
|
82 |
js=f"""
|
83 |
function() {{
|
|
|
102 |
"""
|
103 |
) as demo:
|
104 |
|
|
|
105 |
with gr.Row(elem_classes="main-header"):
|
106 |
with gr.Column():
|
107 |
gr.HTML(f"""
|
|
|
120 |
</div>
|
121 |
""")
|
122 |
|
|
|
123 |
with gr.Row(equal_height=False):
|
|
|
124 |
with gr.Column(scale=1.5, elem_classes="settings-panel"):
|
125 |
gr.Markdown("## Translation Settings")
|
126 |
with gr.Row():
|
|
|
134 |
repetition_penalty = gr.Slider(1.0, 2.0, 1.2, step=0.1, label="Repetition Penalty")
|
135 |
max_tokens = gr.Slider(100, 2000, 400, step=50, label="Max Tokens")
|
136 |
|
|
|
137 |
with gr.Column(scale=2, elem_classes="translation-card"):
|
138 |
gr.Markdown("## Translation Interface")
|
139 |
source_text = gr.Textbox(
|
|
|
153 |
placeholder="Translation will appear here"
|
154 |
)
|
155 |
|
|
|
156 |
with gr.Row():
|
157 |
with gr.Column():
|
158 |
gr.Markdown("### Quick Examples")
|
|
|
167 |
inputs=[source_text, source_lang, target_lang],
|
168 |
)
|
169 |
|
|
|
170 |
def swap_languages(src, tgt):
|
171 |
return tgt, src
|
172 |
|
173 |
def clear_all():
|
174 |
return "", ""
|
175 |
|
176 |
+
swap_btn.click(fn=swap_languages, inputs=[source_lang, target_lang], outputs=[source_lang, target_lang])
|
177 |
+
clear_btn.click(fn=clear_all, outputs=[source_text, translated_text])
|
178 |
+
translate_btn.click(fn=translate_text, inputs=[source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens], outputs=[translated_text])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
demo.load(fn=init_translation_model)
|
180 |
|
181 |
if __name__ == "__main__":
|