Chad commited on
Commit ·
bd095b8
1
Parent(s): 5df60f7
lang togglwdwdwdeeefefefdwdwdwdwd
Browse files
app.py
CHANGED
|
@@ -45,54 +45,75 @@ def generate_reply(user_input, chat_history):
|
|
| 45 |
return chat_history, chat_history, ""
|
| 46 |
|
| 47 |
def toggle_language(language):
|
| 48 |
-
"""Toggle
|
| 49 |
if language == "english":
|
| 50 |
-
return
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
else:
|
| 59 |
-
return
|
| 60 |
-
"
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
with gr.Blocks() as interface:
|
| 69 |
-
language_state = gr.State("english")
|
| 70 |
-
example_state = gr.State([
|
| 71 |
-
"How did the Leaning Tower of Pisa start leaning?",
|
| 72 |
-
"Who built the Colosseum?",
|
| 73 |
-
"What caused the fall of the Roman Empire?",
|
| 74 |
-
"When did World War II begin and end?",
|
| 75 |
-
"What was the significance of the Magna Carta?",
|
| 76 |
-
"Who was the first person to step on the moon?"
|
| 77 |
-
])
|
| 78 |
|
| 79 |
title = gr.Markdown("## History Chatbot")
|
| 80 |
lang_button = gr.Button("FR")
|
| 81 |
user_input = gr.Textbox(placeholder="Enter your question here...")
|
|
|
|
| 82 |
|
| 83 |
chatbot = gr.Chatbot(type="messages")
|
| 84 |
state = gr.State([])
|
| 85 |
-
send_button = gr.Button("Send")
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
lang_button.click(
|
| 93 |
-
fn=
|
| 94 |
inputs=[language_state],
|
| 95 |
-
outputs=[language_state, title, lang_button, user_input,
|
| 96 |
)
|
| 97 |
|
| 98 |
send_button.click(
|
|
|
|
| 45 |
return chat_history, chat_history, ""
|
| 46 |
|
| 47 |
def toggle_language(language):
|
| 48 |
+
"""Toggle UI elements between English and French."""
|
| 49 |
if language == "english":
|
| 50 |
+
return (
|
| 51 |
+
"french", # New language
|
| 52 |
+
"## Chatbot d'histoire", # New title
|
| 53 |
+
"EN", # New language button label
|
| 54 |
+
"Entrez votre question ici...", # New placeholder text
|
| 55 |
+
"Envoyer", # New send button label
|
| 56 |
+
[ # New examples
|
| 57 |
+
"Comment la tour de Pise a-t-elle commencé à pencher ?",
|
| 58 |
+
"Qui a construit le Colisée ?",
|
| 59 |
+
"Quelles sont les causes de la chute de l'Empire romain ?",
|
| 60 |
+
"Quand la Seconde Guerre mondiale a-t-elle commencé et terminé ?",
|
| 61 |
+
"Quelle est l'importance de la Magna Carta ?",
|
| 62 |
+
"Qui était la première personne à marcher sur la lune?"
|
| 63 |
+
]
|
| 64 |
+
)
|
| 65 |
else:
|
| 66 |
+
return (
|
| 67 |
+
"english",
|
| 68 |
+
"## History Chatbot",
|
| 69 |
+
"FR",
|
| 70 |
+
"Enter your question here...",
|
| 71 |
+
"Send",
|
| 72 |
+
[
|
| 73 |
+
"How did the Leaning Tower of Pisa start leaning?",
|
| 74 |
+
"Who built the Colosseum?",
|
| 75 |
+
"What caused the fall of the Roman Empire?",
|
| 76 |
+
"When did World War II begin and end?",
|
| 77 |
+
"What was the significance of the Magna Carta?",
|
| 78 |
+
"Who was the first person to step on the moon?"
|
| 79 |
+
]
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
with gr.Blocks() as interface:
|
| 83 |
+
language_state = gr.State("english") # Store current language
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
title = gr.Markdown("## History Chatbot")
|
| 86 |
lang_button = gr.Button("FR")
|
| 87 |
user_input = gr.Textbox(placeholder="Enter your question here...")
|
| 88 |
+
send_button = gr.Button("Send")
|
| 89 |
|
| 90 |
chatbot = gr.Chatbot(type="messages")
|
| 91 |
state = gr.State([])
|
|
|
|
| 92 |
|
| 93 |
+
# Create examples dynamically
|
| 94 |
+
example_container = gr.Column()
|
| 95 |
+
with example_container:
|
| 96 |
+
example_component = gr.Examples(
|
| 97 |
+
examples=[
|
| 98 |
+
"How did the Leaning Tower of Pisa start leaning?",
|
| 99 |
+
"Who built the Colosseum?",
|
| 100 |
+
"What caused the fall of the Roman Empire?",
|
| 101 |
+
"When did World War II begin and end?",
|
| 102 |
+
"What was the significance of the Magna Carta?",
|
| 103 |
+
"Who was the first person to step on the moon?"
|
| 104 |
+
],
|
| 105 |
+
inputs=user_input
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Toggle language and update all necessary elements
|
| 109 |
+
def update_ui(language):
|
| 110 |
+
new_language, new_title, new_button_text, new_placeholder, new_send_label, new_examples = toggle_language(language)
|
| 111 |
+
return new_language, new_title, new_button_text, new_placeholder, new_send_label, gr.Examples(examples=new_examples, inputs=user_input)
|
| 112 |
|
| 113 |
lang_button.click(
|
| 114 |
+
fn=update_ui,
|
| 115 |
inputs=[language_state],
|
| 116 |
+
outputs=[language_state, title, lang_button, user_input, send_button, example_container]
|
| 117 |
)
|
| 118 |
|
| 119 |
send_button.click(
|