Update app.py
Browse files
app.py
CHANGED
@@ -1,54 +1,65 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
"
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
else:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langdetect import detect
|
3 |
+
from deep_translator import GoogleTranslator
|
4 |
+
import random
|
5 |
+
|
6 |
+
# Sample phrases
|
7 |
+
phrases = {
|
8 |
+
"easy": [
|
9 |
+
"I love learning new languages.",
|
10 |
+
"The sun is shining brightly.",
|
11 |
+
"Books are windows to the world."
|
12 |
+
],
|
13 |
+
"medium": [
|
14 |
+
"Curiosity is the wick in the candle of learning.",
|
15 |
+
"Technology connects the world in real time.",
|
16 |
+
"Imagination is the beginning of creation."
|
17 |
+
],
|
18 |
+
"hard": [
|
19 |
+
"Philosophy begins in wonder and ends in understanding.",
|
20 |
+
"The subconscious mind is a powerful force.",
|
21 |
+
"Language is the roadmap of a culture."
|
22 |
+
]
|
23 |
+
}
|
24 |
+
|
25 |
+
# State
|
26 |
+
current_phrase = None
|
27 |
+
selected_lang = None
|
28 |
+
|
29 |
+
def start_game(difficulty):
|
30 |
+
global current_phrase, selected_lang
|
31 |
+
current_phrase = random.choice(phrases[difficulty])
|
32 |
+
detected = detect(current_phrase)
|
33 |
+
selected_lang = random.choice(['es', 'fr', 'de', 'hi', 'ur', 'ar'])
|
34 |
+
translated = GoogleTranslator(source='en', target=selected_lang).translate(current_phrase)
|
35 |
+
return translated, "Try guessing what this means in English!"
|
36 |
+
|
37 |
+
def check_guess(user_guess):
|
38 |
+
if not current_phrase:
|
39 |
+
return "Click 'Start Game' first!"
|
40 |
+
if user_guess.strip().lower() == current_phrase.strip().lower():
|
41 |
+
return f"โ
Correct! It was: '{current_phrase}'"
|
42 |
else:
|
43 |
+
return f"โ Incorrect! Try again!"
|
44 |
+
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
gr.Markdown("## ๐ Language Guess Game - Remix Edition")
|
47 |
+
|
48 |
+
with gr.Row():
|
49 |
+
difficulty = gr.Radio(["easy", "medium", "hard"], label="Select Difficulty")
|
50 |
+
start_btn = gr.Button("๐ฎ Start Game")
|
51 |
+
|
52 |
+
with gr.Row():
|
53 |
+
translated_text = gr.Textbox(label="Translated Phrase", interactive=False)
|
54 |
+
info = gr.Textbox(label="Instructions", interactive=False)
|
55 |
+
|
56 |
+
with gr.Row():
|
57 |
+
user_guess = gr.Textbox(label="Your English Guess")
|
58 |
+
submit_btn = gr.Button("โ
Submit Guess")
|
59 |
+
|
60 |
+
result = gr.Textbox(label="Result", interactive=False)
|
61 |
+
|
62 |
+
start_btn.click(fn=start_game, inputs=[difficulty], outputs=[translated_text, info])
|
63 |
+
submit_btn.click(fn=check_guess, inputs=[user_guess], outputs=[result])
|
64 |
+
|
65 |
+
demo.launch()
|