Spaces:
Running
Running
set custom language
Browse files
app.py
CHANGED
@@ -18,13 +18,18 @@ LANGUAGES = {
|
|
18 |
"Japanese": "ja",
|
19 |
"Chinese": "zh",
|
20 |
"Hindi": "hi",
|
21 |
-
"Korean": "ko"
|
|
|
22 |
}
|
23 |
|
24 |
-
def extract_wikipedia_content(wiki_url, api_key, model_id, base_url, target_lang, content_format):
|
25 |
"""
|
26 |
Function to extract content from Wikipedia URL (placeholder for now)
|
27 |
"""
|
|
|
|
|
|
|
|
|
28 |
wiki_id = extract_wiki_id(wiki_url)
|
29 |
if not wiki_id:
|
30 |
return "Invalid Wikipedia URL. Please check the URL and try again.", None, None, None, None, {}
|
@@ -50,6 +55,8 @@ def translate_content(content, article_title, artice_summary, content_format,
|
|
50 |
|
51 |
llm_client = init_llm_client(api_key, base_url=base_url)
|
52 |
|
|
|
|
|
53 |
translation_prompt = get_translate_prompt(
|
54 |
article_title=article_title,
|
55 |
artice_summary=artice_summary,
|
@@ -74,19 +81,25 @@ def translate_content(content, article_title, artice_summary, content_format,
|
|
74 |
|
75 |
return "Error: Translation output not found in the response."
|
76 |
|
77 |
-
def translate_section(section_content, article_title, article_summary, content_format, target_lang, api_key, model_id, base_url):
|
78 |
"""
|
79 |
Translates a single section of the Wikipedia article
|
80 |
"""
|
81 |
if not section_content or not api_key:
|
82 |
return "Please provide content and API key for translation."
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
return translate_content(
|
85 |
content=section_content,
|
86 |
article_title=article_title,
|
87 |
artice_summary=article_summary,
|
88 |
content_format=content_format,
|
89 |
-
target_lang=
|
90 |
api_key=api_key,
|
91 |
model_id=model_id,
|
92 |
base_url=base_url
|
@@ -160,6 +173,12 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
|
160 |
gr.update(visible=not new_expanded) # Control visibility of the show button
|
161 |
)
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
with gr.Row() as main_layout:
|
164 |
# Sidebar for configuration
|
165 |
with gr.Column(scale=1, visible=True) as sidebar:
|
@@ -193,6 +212,20 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
|
193 |
label="Target Language",
|
194 |
)
|
195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
content_format = gr.Radio(
|
197 |
choices=["Text", "XML"],
|
198 |
value="XML",
|
@@ -273,6 +306,7 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
|
273 |
aticle_summary,
|
274 |
content_format,
|
275 |
target_language,
|
|
|
276 |
api_key,
|
277 |
model_id,
|
278 |
base_url
|
@@ -283,7 +317,7 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
|
283 |
# Connect the extract button to the function
|
284 |
extract_button.click(
|
285 |
fn=extract_wikipedia_content,
|
286 |
-
inputs=[wiki_url, api_key, model_id, base_url, target_language, content_format],
|
287 |
outputs=[
|
288 |
output,
|
289 |
article_pageid,
|
|
|
18 |
"Japanese": "ja",
|
19 |
"Chinese": "zh",
|
20 |
"Hindi": "hi",
|
21 |
+
"Korean": "ko",
|
22 |
+
"Custom": "custom" # Add custom option
|
23 |
}
|
24 |
|
25 |
+
def extract_wikipedia_content(wiki_url, api_key, model_id, base_url, target_lang, custom_lang, content_format):
|
26 |
"""
|
27 |
Function to extract content from Wikipedia URL (placeholder for now)
|
28 |
"""
|
29 |
+
# Use custom language if selected
|
30 |
+
if target_lang == "custom" and custom_lang:
|
31 |
+
target_lang = custom_lang
|
32 |
+
|
33 |
wiki_id = extract_wiki_id(wiki_url)
|
34 |
if not wiki_id:
|
35 |
return "Invalid Wikipedia URL. Please check the URL and try again.", None, None, None, None, {}
|
|
|
55 |
|
56 |
llm_client = init_llm_client(api_key, base_url=base_url)
|
57 |
|
58 |
+
# Use the target_lang as is - it should already be properly resolved
|
59 |
+
# by the calling function (either a language code or custom value)
|
60 |
translation_prompt = get_translate_prompt(
|
61 |
article_title=article_title,
|
62 |
artice_summary=artice_summary,
|
|
|
81 |
|
82 |
return "Error: Translation output not found in the response."
|
83 |
|
84 |
+
def translate_section(section_content, article_title, article_summary, content_format, target_lang, custom_lang, api_key, model_id, base_url):
|
85 |
"""
|
86 |
Translates a single section of the Wikipedia article
|
87 |
"""
|
88 |
if not section_content or not api_key:
|
89 |
return "Please provide content and API key for translation."
|
90 |
|
91 |
+
# Use custom language if selected
|
92 |
+
if target_lang == "custom" and custom_lang:
|
93 |
+
actual_lang = custom_lang
|
94 |
+
else:
|
95 |
+
actual_lang = target_lang
|
96 |
+
|
97 |
return translate_content(
|
98 |
content=section_content,
|
99 |
article_title=article_title,
|
100 |
artice_summary=article_summary,
|
101 |
content_format=content_format,
|
102 |
+
target_lang=actual_lang,
|
103 |
api_key=api_key,
|
104 |
model_id=model_id,
|
105 |
base_url=base_url
|
|
|
173 |
gr.update(visible=not new_expanded) # Control visibility of the show button
|
174 |
)
|
175 |
|
176 |
+
# Function to show/hide custom language input based on selection
|
177 |
+
def toggle_custom_language(target_lang):
|
178 |
+
if target_lang == "Custom":
|
179 |
+
return gr.update(visible=True)
|
180 |
+
return gr.update(visible=False)
|
181 |
+
|
182 |
with gr.Row() as main_layout:
|
183 |
# Sidebar for configuration
|
184 |
with gr.Column(scale=1, visible=True) as sidebar:
|
|
|
212 |
label="Target Language",
|
213 |
)
|
214 |
|
215 |
+
custom_language = gr.Textbox(
|
216 |
+
label="Custom Language",
|
217 |
+
placeholder="Enter language name (e.g., Swedish, Dutch, etc.)",
|
218 |
+
visible=False,
|
219 |
+
info="Specify your desired language if not in the list above"
|
220 |
+
)
|
221 |
+
|
222 |
+
# Connect the dropdown to show/hide custom language input
|
223 |
+
target_language.change(
|
224 |
+
fn=toggle_custom_language,
|
225 |
+
inputs=[target_language],
|
226 |
+
outputs=[custom_language]
|
227 |
+
)
|
228 |
+
|
229 |
content_format = gr.Radio(
|
230 |
choices=["Text", "XML"],
|
231 |
value="XML",
|
|
|
306 |
aticle_summary,
|
307 |
content_format,
|
308 |
target_language,
|
309 |
+
custom_language,
|
310 |
api_key,
|
311 |
model_id,
|
312 |
base_url
|
|
|
317 |
# Connect the extract button to the function
|
318 |
extract_button.click(
|
319 |
fn=extract_wikipedia_content,
|
320 |
+
inputs=[wiki_url, api_key, model_id, base_url, target_language, custom_language, content_format],
|
321 |
outputs=[
|
322 |
output,
|
323 |
article_pageid,
|