arad1367 commited on
Commit
57784dd
1 Parent(s): c1c46a2

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +104 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import gradio as gr
3
+
4
+ # List of countries
5
+ countries = [
6
+ "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda",
7
+ "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain",
8
+ "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia",
9
+ "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso",
10
+ "Burundi", "Cabo Verde", "Cambodia", "Cameroon", "Canada", "Central African Republic",
11
+ "Chad", "Chile", "China", "Colombia", "Comoros", "Congo", "Costa Rica", "Croatia",
12
+ "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic",
13
+ "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
14
+ "Eswatini", "Ethiopia", "Fiji", "Finland", "France", "Gabon", "Gambia", "Georgia", "Germany",
15
+ "Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti",
16
+ "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel",
17
+ "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, North",
18
+ "Korea, South", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia",
19
+ "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Madagascar", "Malawi", "Malaysia",
20
+ "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico",
21
+ "Micronesia", "Moldova", "Monaco", "Mongolia", "Montenegro", "Morocco", "Mozambique",
22
+ "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger",
23
+ "Nigeria", "North Macedonia", "Norway", "Oman", "Pakistan", "Palau", "Palestine", "Panama",
24
+ "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar",
25
+ "Romania", "Russia", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines",
26
+ "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia",
27
+ "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands",
28
+ "Somalia", "South Africa", "South Sudan", "Spain", "Sri Lanka", "Sudan", "Suriname",
29
+ "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo",
30
+ "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu", "Uganda",
31
+ "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan",
32
+ "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe"
33
+ ]
34
+
35
+ # Initialize game variables
36
+ selected_country = ""
37
+ attempts_left = 0
38
+ guessed_letters = []
39
+ player_name = ""
40
+
41
+ def start_game(name):
42
+ global selected_country, attempts_left, guessed_letters, player_name
43
+ player_name = name
44
+ selected_country = random.choice(countries).upper()
45
+ attempts_left = len(selected_country)
46
+ guessed_letters = ["_"] * len(selected_country)
47
+ return update_display()
48
+
49
+ def update_display():
50
+ return f"Country: {' '.join(guessed_letters)}", f"Guesses left: {attempts_left}", ""
51
+
52
+ def guess_letter(letter):
53
+ global attempts_left
54
+ if attempts_left == 0:
55
+ return "No more attempts left. Please restart the game.", "", "Try again."
56
+
57
+ letter = letter.upper()
58
+ if letter in selected_country:
59
+ for i in range(len(selected_country)):
60
+ if selected_country[i] == letter:
61
+ guessed_letters[i] = letter
62
+ else:
63
+ attempts_left -= 1
64
+
65
+ if "_" not in guessed_letters:
66
+ return f"Country: {' '.join(guessed_letters)}", f"Guesses left: {attempts_left}", f"Congratulations {player_name}! You won!"
67
+
68
+ if attempts_left == 0:
69
+ return f"Country: {' '.join(guessed_letters)}", f"Guesses left: {attempts_left}", f"Sorry {player_name}, you lost. The country was {selected_country}."
70
+
71
+ return update_display()
72
+
73
+ def restart_game():
74
+ return start_game(player_name)
75
+
76
+ footer = """
77
+ <div style="text-align: center; margin-top: 20px;">
78
+ <a href="https://www.linkedin.com/in/pejman-ebrahimi-4a60151a7/" target="_blank">LinkedIn</a> |
79
+ <a href="https://github.com/arad1367/Visual_QA_MiniCPM-Llama3-V-2_5_GradioApp" target="_blank">GitHub</a> |
80
+ <a href="https://arad1367.pythonanywhere.com/" target="_blank">Live demo of my PhD defense</a>
81
+ <br>
82
+ Made with 💖 by Pejman Ebrahimi
83
+ </div>
84
+ """
85
+
86
+ with gr.Blocks(theme='abidlabs/dracula_revamped') as demo:
87
+ gr.Markdown("### Country Guessing Game")
88
+ with gr.Row():
89
+ name_input = gr.Textbox(label="Enter your name:", placeholder="Name")
90
+ start_button = gr.Button("Start Game")
91
+ country_display = gr.Textbox(label="Country", interactive=False)
92
+ attempts_display = gr.Textbox(label="Guesses left", interactive=False)
93
+ result_display = gr.Textbox(label="Result", interactive=False)
94
+ letter_input = gr.Textbox(label="Enter a letter:", max_lines=1, placeholder="Letter")
95
+ guess_button = gr.Button("Guess")
96
+ restart_button = gr.Button("Restart Game")
97
+
98
+ start_button.click(start_game, inputs=name_input, outputs=[country_display, attempts_display, result_display])
99
+ guess_button.click(guess_letter, inputs=letter_input, outputs=[country_display, attempts_display, result_display])
100
+ restart_button.click(restart_game, inputs=None, outputs=[country_display, attempts_display, result_display])
101
+
102
+ gr.HTML(footer)
103
+
104
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ -e .