rahideer commited on
Commit
f44fa75
ยท
verified ยท
1 Parent(s): f6096de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -53
app.py CHANGED
@@ -1,54 +1,65 @@
1
- import streamlit as st
2
- from logic import *
3
-
4
- st.set_page_config(page_title="20Q Chat Game", layout="centered")
5
-
6
- if "session" not in st.session_state:
7
- st.session_state.session = initialize_session()
8
-
9
- st.title("๐ŸŽฎ 20 Questions Chat Game")
10
- st.markdown("Guessing game with a smarter brain! Pick a category and start answering.")
11
-
12
- # Select Category (once)
13
- if st.session_state.session["category"] is None:
14
- st.session_state.session["category"] = st.selectbox(
15
- "Choose a category:", ["Person", "Object", "Place", "Animal", "Concept"]
16
- )
17
- st.stop()
18
-
19
- # Main Chat Loop
20
- session = st.session_state.session
21
- history = session["question_history"]
22
-
23
- # Display chat
24
- for i, turn in enumerate(history):
25
- st.chat_message("assistant").write(f"๐Ÿค– Question {i+1}: {turn['question']}")
26
- st.chat_message("user").write(f"๐Ÿ™‹ Answer: {turn['answer']}")
27
-
28
- if session["guessed"] or session["turn"] >= session["max_turns"]:
29
- guess = make_guess(history, session["category"])
30
- st.chat_message("assistant").write(f"๐Ÿง  My final guess: **{guess}**")
31
- st.stop()
32
-
33
- # Ask next question
34
- if session["turn"] == 0 or st.button("Next Question"):
35
- if session["turn"] == 0:
36
- q = ask_ai([], session["category"])
 
 
 
 
 
37
  else:
38
- q = ask_ai(history, session["category"])
39
- st.session_state.last_question = q
40
- st.session_state.turning = True
41
- st.rerun()
42
-
43
- # Answer input
44
- if "last_question" in st.session_state and st.session_state.turning:
45
- q = st.session_state.last_question
46
- st.chat_message("assistant").write(f"๐Ÿค– Question {session['turn'] + 1}: {q}")
47
- answer = st.text_input("Your answer (yes/no/other):")
48
-
49
- if answer:
50
- translated, lang = translate_to_english(answer)
51
- history.append({"question": q, "answer": translated})
52
- session["turn"] += 1
53
- st.session_state.turning = False
54
- st.rerun()
 
 
 
 
 
 
 
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()