Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,22 +4,30 @@ import sys
|
|
4 |
import base64
|
5 |
import threading
|
6 |
from huggingface_hub import login, hf_hub_download
|
7 |
-
import torch
|
8 |
|
9 |
login(token=os.environ["HF_TOKEN"])
|
10 |
repo_id = os.environ["REPO_ID"]
|
11 |
|
|
|
12 |
torch.set_num_threads(1)
|
13 |
if torch.cuda.is_available():
|
14 |
torch.backends.cudnn.benchmark = True
|
15 |
|
16 |
try:
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
except Exception as e:
|
24 |
print(f"Error downloading files: {e}")
|
25 |
|
@@ -44,14 +52,9 @@ def init_translation_model():
|
|
44 |
print("Failed to load model")
|
45 |
|
46 |
def translate_text(source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens):
|
47 |
-
return chat_interface(
|
48 |
-
source_text, source_lang, target_lang,
|
49 |
-
temperature, top_k, repetition_penalty, max_tokens
|
50 |
-
)
|
51 |
|
52 |
languages = ["English", "Hindi", "Bengali", "Tamil", "Telugu", "Kannada", "Panjabi"]
|
53 |
-
css_path = "static/style.css"
|
54 |
-
custom_css = open(css_path, encoding="utf-8").read() if os.path.isfile(css_path) else ""
|
55 |
|
56 |
theme_lock_css = """
|
57 |
.gradio-container .theme-toggle,
|
@@ -70,7 +73,7 @@ body, .gradio-container {
|
|
70 |
}
|
71 |
"""
|
72 |
|
73 |
-
combined_css =
|
74 |
|
75 |
locked_theme = gr.themes.Monochrome(primary_hue="blue", secondary_hue="slate", neutral_hue="slate").set(
|
76 |
background_fill_primary="#0a1628",
|
@@ -83,9 +86,11 @@ locked_theme = gr.themes.Monochrome(primary_hue="blue", secondary_hue="slate", n
|
|
83 |
body_text_color="#e6eef8"
|
84 |
)
|
85 |
|
|
|
86 |
with gr.Blocks(
|
87 |
title="Madhuram Translation - MaruthLabs",
|
88 |
-
css=combined_css,
|
|
|
89 |
js=f"""
|
90 |
function() {{
|
91 |
{f'document.head.insertAdjacentHTML("beforeend", `{FAVICON_HTML}`);' if FAVICON_HTML else ''}
|
@@ -169,23 +174,9 @@ with gr.Blocks(
|
|
169 |
def clear_all():
|
170 |
return "", ""
|
171 |
|
172 |
-
swap_btn.click(
|
173 |
-
|
174 |
-
|
175 |
-
outputs=[source_lang, target_lang]
|
176 |
-
)
|
177 |
-
|
178 |
-
clear_btn.click(
|
179 |
-
fn=clear_all,
|
180 |
-
outputs=[source_text, translated_text]
|
181 |
-
)
|
182 |
-
|
183 |
-
translate_btn.click(
|
184 |
-
fn=translate_text,
|
185 |
-
inputs=[source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens],
|
186 |
-
outputs=[translated_text]
|
187 |
-
)
|
188 |
-
|
189 |
demo.load(fn=init_translation_model)
|
190 |
|
191 |
if __name__ == "__main__":
|
|
|
4 |
import base64
|
5 |
import threading
|
6 |
from huggingface_hub import login, hf_hub_download
|
|
|
7 |
|
8 |
login(token=os.environ["HF_TOKEN"])
|
9 |
repo_id = os.environ["REPO_ID"]
|
10 |
|
11 |
+
import torch
|
12 |
torch.set_num_threads(1)
|
13 |
if torch.cuda.is_available():
|
14 |
torch.backends.cudnn.benchmark = True
|
15 |
|
16 |
try:
|
17 |
+
files_to_download = ["generate.py", "model.py", "hfmod_sft.py"]
|
18 |
+
|
19 |
+
for filename in files_to_download:
|
20 |
+
try:
|
21 |
+
file_path = hf_hub_download(
|
22 |
+
repo_id=repo_id,
|
23 |
+
filename=filename,
|
24 |
+
token=os.environ["HF_TOKEN"]
|
25 |
+
)
|
26 |
+
os.system(f"cp {file_path} ./{filename}")
|
27 |
+
print(f"Downloaded: {filename}")
|
28 |
+
except Exception as e:
|
29 |
+
print(f"Warning: Could not download {filename}: {e}")
|
30 |
+
|
31 |
except Exception as e:
|
32 |
print(f"Error downloading files: {e}")
|
33 |
|
|
|
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 |
+
return chat_interface(source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens)
|
|
|
|
|
|
|
56 |
|
57 |
languages = ["English", "Hindi", "Bengali", "Tamil", "Telugu", "Kannada", "Panjabi"]
|
|
|
|
|
58 |
|
59 |
theme_lock_css = """
|
60 |
.gradio-container .theme-toggle,
|
|
|
73 |
}
|
74 |
"""
|
75 |
|
76 |
+
combined_css = theme_lock_css
|
77 |
|
78 |
locked_theme = gr.themes.Monochrome(primary_hue="blue", secondary_hue="slate", neutral_hue="slate").set(
|
79 |
background_fill_primary="#0a1628",
|
|
|
86 |
body_text_color="#e6eef8"
|
87 |
)
|
88 |
|
89 |
+
# Create Gradio interface
|
90 |
with gr.Blocks(
|
91 |
title="Madhuram Translation - MaruthLabs",
|
92 |
+
css=combined_css,
|
93 |
+
theme=locked_theme,
|
94 |
js=f"""
|
95 |
function() {{
|
96 |
{f'document.head.insertAdjacentHTML("beforeend", `{FAVICON_HTML}`);' if FAVICON_HTML else ''}
|
|
|
174 |
def clear_all():
|
175 |
return "", ""
|
176 |
|
177 |
+
swap_btn.click(fn=swap_languages, inputs=[source_lang, target_lang], outputs=[source_lang, target_lang])
|
178 |
+
clear_btn.click(fn=clear_all, outputs=[source_text, translated_text])
|
179 |
+
translate_btn.click(fn=translate_text, inputs=[source_text, source_lang, target_lang, temperature, top_k, repetition_penalty, max_tokens], outputs=[translated_text])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
demo.load(fn=init_translation_model)
|
181 |
|
182 |
if __name__ == "__main__":
|