Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
-
# Import necessary libraries
|
2 |
import gradio as gr
|
3 |
-
import random
|
4 |
import openai
|
|
|
5 |
import os
|
6 |
|
7 |
-
#
|
8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
9 |
|
10 |
# Updated list of 5-letter food words
|
@@ -14,7 +13,6 @@ food_words = [
|
|
14 |
"beans", "candy", "wafer", "spice", "honey"
|
15 |
]
|
16 |
|
17 |
-
# Global state variables
|
18 |
answer = random.choice(food_words).lower()
|
19 |
current_guess = ""
|
20 |
attempts = 0
|
@@ -62,22 +60,22 @@ def submit_guess():
|
|
62 |
current_guess = ""
|
63 |
return generate_grid_html(), "Enter your next guess."
|
64 |
|
65 |
-
def
|
66 |
-
"""
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
current_guess = current_guess[:-1]
|
71 |
-
elif len(current_guess) < 5 and key.isalpha():
|
72 |
-
current_guess += key.lower()
|
73 |
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
75 |
return submit_guess()
|
76 |
-
|
77 |
-
return generate_grid_html(), f"Current guess: {current_guess}"
|
78 |
|
79 |
def restart_game():
|
80 |
-
"""Reset the game to the initial state."""
|
81 |
global answer, current_guess, attempts, grid_content
|
82 |
answer = random.choice(food_words).lower()
|
83 |
current_guess = ""
|
@@ -85,17 +83,6 @@ def restart_game():
|
|
85 |
grid_content = [[{"letter": "", "color": ""} for _ in range(5)] for _ in range(6)]
|
86 |
return generate_grid_html(), "Game restarted. Enter a 5-letter word."
|
87 |
|
88 |
-
def reveal_answer():
|
89 |
-
"""Reveal the answer to the user."""
|
90 |
-
return generate_grid_html(), f"The answer was: {answer.upper()}."
|
91 |
-
|
92 |
-
def transcribe_audio(audio):
|
93 |
-
"""Use OpenAI Whisper to transcribe audio input."""
|
94 |
-
audio_file = open(audio, "rb")
|
95 |
-
transcription = openai.Audio.transcribe("whisper-1", audio_file)
|
96 |
-
return transcription["text"]
|
97 |
-
|
98 |
-
# Gradio interface with CSS for layout
|
99 |
with gr.Blocks(css="""
|
100 |
.grid-row { display: flex; justify-content: center; margin-bottom: 5px; }
|
101 |
.grid-cell { width: 50px; height: 50px; border: 1px solid #ccc; margin: 2px;
|
@@ -106,35 +93,12 @@ with gr.Blocks(css="""
|
|
106 |
.grey { background-color: lightgrey; }
|
107 |
""") as demo:
|
108 |
gr.Markdown("# π² Foodle: Guess the Food Dish!")
|
109 |
-
gr.Markdown("Test your culinary knowledge and guess the 5-letter dish!")
|
110 |
-
gr.Markdown("**Developed by [Venkat](https://www.linkedin.com/in/venkataraghavansrinivasan/)**")
|
111 |
-
|
112 |
grid_display = gr.HTML(generate_grid_html())
|
113 |
status_display = gr.Textbox("Enter your next guess.", interactive=False)
|
114 |
|
115 |
-
|
116 |
-
audio_input =
|
117 |
-
transcribe_button = gr.Button("Transcribe")
|
118 |
-
transcribe_button.click(transcribe_audio, inputs=audio_input, outputs=status_display)
|
119 |
-
|
120 |
-
# Keyboard Layout
|
121 |
-
with gr.Row(variant="compact"):
|
122 |
-
for key in "qwertyuiop":
|
123 |
-
gr.Button(key).click(lambda k=key: handle_key_press(k), outputs=[grid_display, status_display])
|
124 |
-
|
125 |
-
with gr.Row(variant="compact"):
|
126 |
-
for key in "asdfghjkl":
|
127 |
-
gr.Button(key).click(lambda k=key: handle_key_press(k), outputs=[grid_display, status_display])
|
128 |
-
|
129 |
-
with gr.Row(variant="compact"):
|
130 |
-
for key in "zxcvbnm":
|
131 |
-
gr.Button(key).click(lambda k=key: handle_key_press(k), outputs=[grid_display, status_display])
|
132 |
|
133 |
-
|
134 |
-
with gr.Row():
|
135 |
-
gr.Button("Backspace").click(lambda: handle_key_press("Backspace"), outputs=[grid_display, status_display])
|
136 |
-
gr.Button("Restart Game").click(restart_game, outputs=[grid_display, status_display])
|
137 |
-
gr.Button("Reveal Answer").click(reveal_answer, outputs=[grid_display, status_display])
|
138 |
|
139 |
-
# Launch the Gradio app
|
140 |
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import openai
|
3 |
+
import random
|
4 |
import os
|
5 |
|
6 |
+
# Set your OpenAI API key securely using environment variables
|
7 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
8 |
|
9 |
# Updated list of 5-letter food words
|
|
|
13 |
"beans", "candy", "wafer", "spice", "honey"
|
14 |
]
|
15 |
|
|
|
16 |
answer = random.choice(food_words).lower()
|
17 |
current_guess = ""
|
18 |
attempts = 0
|
|
|
60 |
current_guess = ""
|
61 |
return generate_grid_html(), "Enter your next guess."
|
62 |
|
63 |
+
def transcribe_audio(audio_file):
|
64 |
+
"""Use OpenAI Whisper to transcribe audio."""
|
65 |
+
with open(audio_file, "rb") as f:
|
66 |
+
transcript = openai.Audio.transcribe("whisper-1", f)
|
67 |
+
return transcript["text"].strip().lower()
|
|
|
|
|
|
|
68 |
|
69 |
+
def handle_audio_input(audio_file):
|
70 |
+
"""Handle the audio input and check the guess."""
|
71 |
+
global current_guess
|
72 |
+
spoken_text = transcribe_audio(audio_file)
|
73 |
+
if len(spoken_text) == 5 and spoken_text.isalpha():
|
74 |
+
current_guess = spoken_text
|
75 |
return submit_guess()
|
76 |
+
return generate_grid_html(), "Please say a valid 5-letter word."
|
|
|
77 |
|
78 |
def restart_game():
|
|
|
79 |
global answer, current_guess, attempts, grid_content
|
80 |
answer = random.choice(food_words).lower()
|
81 |
current_guess = ""
|
|
|
83 |
grid_content = [[{"letter": "", "color": ""} for _ in range(5)] for _ in range(6)]
|
84 |
return generate_grid_html(), "Game restarted. Enter a 5-letter word."
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
with gr.Blocks(css="""
|
87 |
.grid-row { display: flex; justify-content: center; margin-bottom: 5px; }
|
88 |
.grid-cell { width: 50px; height: 50px; border: 1px solid #ccc; margin: 2px;
|
|
|
93 |
.grey { background-color: lightgrey; }
|
94 |
""") as demo:
|
95 |
gr.Markdown("# π² Foodle: Guess the Food Dish!")
|
|
|
|
|
|
|
96 |
grid_display = gr.HTML(generate_grid_html())
|
97 |
status_display = gr.Textbox("Enter your next guess.", interactive=False)
|
98 |
|
99 |
+
audio_input = gr.Audio(type="filepath", label="Speak your guess")
|
100 |
+
audio_input.change(handle_audio_input, inputs=audio_input, outputs=[grid_display, status_display])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
+
gr.Button("Restart Game").click(restart_game, outputs=[grid_display, status_display])
|
|
|
|
|
|
|
|
|
103 |
|
|
|
104 |
demo.launch()
|