chomayouni commited on
Commit
6455751
·
1 Parent(s): f7343c3

Added Application file

Browse files
Files changed (1) hide show
  1. sgg_app.py +95 -0
sgg_app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # def game(difficulty, generate_song, artist_choice, submit_answer):
4
+ # if generate_song:
5
+ # # Generate the song and the options based on the difficulty
6
+ # # In the actual implementation, you would generate the song text and the options based on the difficulty
7
+ # if difficulty == "Demo":
8
+ # song_text = "Generated song text for Demo"
9
+ # options = ["Artist 1", "Artist 2", "Artist 3", "Artist 4"]
10
+ # elif difficulty == "Medium":
11
+ # song_text = "Generated song text for Medium"
12
+ # options = ["Artist 5", "Artist 6", "Artist 7", "Artist 8"]
13
+ # else: # Hard
14
+ # song_text = "Generated song text for Hard"
15
+ # options = ["Artist 9", "Artist 10", "Artist 11", "Artist 12"]
16
+ # return {"Generated Song": song_text, "Options": options}
17
+ # elif submit_answer:
18
+ # # Check the selected artist and return whether it's correct
19
+ # correct_answer = "Artist 1" # Placeholder
20
+ # return {"Correct Answer": correct_answer == artist_choice}
21
+
22
+ # game_interface = gr.Interface(
23
+ # fn=game,
24
+ # inputs=[
25
+ # gr.Radio(["Demo", "Medium", "Hard"], label="Difficulty",
26
+ # info="The higher the difficulty makes it so that the options for the artists are more similar to one another?"),
27
+ # gr.Button("Generate Song"),
28
+ # gr.Radio(["A", "B", "C", "D"], label="Multi-Choice Options",
29
+ # info="Select the artist that you suspect is the correct artist for the song."),
30
+ # gr.Button("Submit Answer"),
31
+ # ],
32
+
33
+ # outputs=[
34
+ # gr.Textbox(label="Generated Song"),
35
+ # gr.Textbox(label="Options"),
36
+ # gr.Textbox(label="Correct Answer"),
37
+ # ],
38
+
39
+ # title="Song Generator Guessing Game",
40
+ # )
41
+
42
+ # game_interface.launch()
43
+
44
+ def generate_song(state, difficulty, generate_song):
45
+ if generate_song:
46
+ if not difficulty:
47
+ song_text = "Please select a difficulty level before generating a song."
48
+ return state, song_text, ""
49
+ # Generate the song and the options based on the difficulty
50
+ if difficulty == "Demo":
51
+ song_text = "Generated song text for Demo"
52
+ options = ["Artist 1", "Artist 2", "Artist 3", "Artist 4"]
53
+ elif difficulty == "Medium":
54
+ song_text = "Generated song text for Medium"
55
+ options = ["Artist 5", "Artist 6", "Artist 7", "Artist 8"]
56
+ else: # Hard
57
+ song_text = "Generated song text for Hard"
58
+ options = ["Artist 9", "Artist 10", "Artist 11", "Artist 12"]
59
+ state['options'] = options
60
+ state['timer_finished'] = False
61
+ timer_script = "<div id='progress-bar' style='width: 100%; background-color: #f3f3f3; border: 1px solid #bbb;'><div id='progress' style='height: 20px; width: 0%; background-color: #007bff;'></div></div><script>function startTimer() {var time = 30; var timer = setInterval(function() {time--; document.getElementById('progress').style.width = (time / 30 * 100) + '%'; if (time <= 0) {clearInterval(timer);}}, 1000);}</script>"
62
+ return state, song_text, ', '.join(options), timer_script
63
+
64
+ def submit_answer(state, artist_choice, submit_answer):
65
+ if submit_answer:
66
+ if not artist_choice:
67
+ correct_answer = "Please select an artist before submitting an answer."
68
+ return {"Error": "Please select an artist before submitting an answer."}
69
+ # Check the selected artist and return whether it's correct
70
+ correct_answer = state['options'][0] # Placeholder
71
+ return {"Correct Answer": correct_answer == artist_choice}
72
+
73
+ with gr.Blocks(title="Song Genorator Guessing Game") as game_interface:
74
+ state = gr.State({'options': []})
75
+ difficulty = gr.Radio(["Demo", "Medium", "Hard"], label="Difficulty")
76
+ generate_song_button = gr.Button("Generate Song")
77
+ artist_choice_display = gr.Textbox(interactive=False, label="Multiple-Choice Options")
78
+ artist_choice = gr.Radio(["A", "B", "C", "D"], label="Updated Options", info="Select the artist that you suspect is the correct artist for the song.")
79
+ timer = gr.HTML("<div id='progress-bar' style='width: 100%; background-color: #f3f3f3; border: 1px solid #bbb;'><div id='progress' style='height: 20px; width: 0%; background-color: #007bff;'></div></div><script>function startTimer() {var time = 30; var timer = setInterval(function() {time--; document.getElementById('progress').style.width = (time / 30 * 100) + '%'; if (time <= 0) {clearInterval(timer);}}, 1000);}</script>", label="Timer")
80
+ submit_answer_button = gr.Button("Submit Answer")
81
+ generated_song = gr.Textbox(label="Generated Song")
82
+ correct_answer = gr.Textbox(label="Correct Answer")
83
+
84
+ generate_song_button.click(
85
+ generate_song,
86
+ [state, difficulty, generate_song_button],
87
+ [state, generated_song, artist_choice_display, timer]
88
+ )
89
+ submit_answer_button.click(
90
+ submit_answer,
91
+ [state, artist_choice, submit_answer_button],
92
+ [correct_answer]
93
+ )
94
+
95
+ game_interface.launch()