Spaces:
Sleeping
Sleeping
Jonathan Bejarano
commited on
Commit
Β·
a58fa96
1
Parent(s):
5bcdc44
basic functionality
Browse files- app.py +88 -5
- requirements.txt +2 -0
app.py
CHANGED
|
@@ -1,5 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def respond(
|
|
@@ -14,7 +73,11 @@ def respond(
|
|
| 14 |
"""
|
| 15 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
| 20 |
|
|
@@ -37,7 +100,13 @@ def respond(
|
|
| 37 |
token = choices[0].delta.content
|
| 38 |
|
| 39 |
response += token
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
"""
|
|
@@ -46,11 +115,25 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
|
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
type="messages",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
gr.Slider(
|
|
|
|
| 54 |
minimum=0.1,
|
| 55 |
maximum=1.0,
|
| 56 |
value=0.95,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import re
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# List of countries for the game
|
| 8 |
+
COUNTRIES = [
|
| 9 |
+
"Afghanistan", "Albania", "Algeria", "Angola", "Argentina", "Armenia", "Australia", "Austria",
|
| 10 |
+
"Bangladesh", "Belgium", "Belize", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil",
|
| 11 |
+
"Bulgaria", "Burma", "Burundi", "Cambodia", "Canada", "Central African Republic", "Chad", "Chile",
|
| 12 |
+
"China", "Colombia", "Costa Rica", "Croatia", "Cuba", "Czech Republic", "Democratic Republic of the Congo",
|
| 13 |
+
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Estonia", "Ethiopia", "Fiji",
|
| 14 |
+
"Finland", "France", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala", "Guinea",
|
| 15 |
+
"Guyana", "Haiti", "Honduras", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland",
|
| 16 |
+
"Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kenya", "Kuwait", "Kyrgyzstan", "Laos", "Latvia",
|
| 17 |
+
"Lebanon", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Madagascar",
|
| 18 |
+
"Malaysia", "Mali", "Malta", "Mexico", "Moldova", "Monaco", "Mongolia", "Montenegro", "Morocco",
|
| 19 |
+
"Mozambique", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "North Korea",
|
| 20 |
+
"Norway", "Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea", "Paraguay", "Peru",
|
| 21 |
+
"Philippines", "Poland", "Portugal", "Qatar", "Republic of the Congo", "Romania", "Russia", "Rwanda",
|
| 22 |
+
"Samoa", "Saudi Arabia", "Serbia", "Singapore", "Slovakia", "South Korea", "Slovenia", "Somalia",
|
| 23 |
+
"South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland",
|
| 24 |
+
"Syria", "Tajikistan", "Tanzania", "Thailand", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey",
|
| 25 |
+
"Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States",
|
| 26 |
+
"Uruguay", "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe"
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
def get_system_message_with_country():
|
| 30 |
+
"""Generate a system message with a randomly selected country"""
|
| 31 |
+
selected_country = random.choice(COUNTRIES)
|
| 32 |
+
return f"""You are a friendly geography game host playing 20 questions with students. You are thinking of the country: {selected_country}
|
| 33 |
+
|
| 34 |
+
RULES:
|
| 35 |
+
1. NEVER reveal the country name ({selected_country}) in your responses
|
| 36 |
+
2. Answer only 'Yes' or 'No' to their questions
|
| 37 |
+
3. Keep track of how many questions they've asked
|
| 38 |
+
4. When they correctly guess {selected_country}, respond with: 'Congratulations! The country was <<{selected_country}>>'
|
| 39 |
+
5. If they reach 20 questions without guessing correctly, respond with: 'Game over! The country was <<{selected_country}>>'
|
| 40 |
+
6. Be encouraging and give helpful hints through your yes/no answers"""
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def extract_country_name(response):
|
| 44 |
+
"""Extract country name from response using the <<COUNTRY_NAME>> format"""
|
| 45 |
+
match = re.search(r'<<(.+?)>>', response)
|
| 46 |
+
if match:
|
| 47 |
+
return match.group(1)
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
def format_game_result(response):
|
| 51 |
+
"""Format the game result with proper styling"""
|
| 52 |
+
country = extract_country_name(response)
|
| 53 |
+
if not country:
|
| 54 |
+
return response
|
| 55 |
+
|
| 56 |
+
if "Congratulations" in response:
|
| 57 |
+
return f"π **Congratulations!** You correctly guessed **{country}**! Well done! π\n\nWould you like to play another round?"
|
| 58 |
+
elif "Game over" in response:
|
| 59 |
+
return f"π **Game Over!** You've used all 20 questions. The country I was thinking of was **{country}**. π\n\nBetter luck next time! Would you like to try again?"
|
| 60 |
+
|
| 61 |
+
return response
|
| 62 |
|
| 63 |
|
| 64 |
def respond(
|
|
|
|
| 73 |
"""
|
| 74 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 75 |
"""
|
| 76 |
+
client = InferenceClient(token=hf_token.token, model="meta-llama/Llama-3.2-3B-Instruct")
|
| 77 |
+
|
| 78 |
+
# Generate a new system message with random country for new conversations
|
| 79 |
+
if not history:
|
| 80 |
+
system_message = get_system_message_with_country()
|
| 81 |
|
| 82 |
messages = [{"role": "system", "content": system_message}]
|
| 83 |
|
|
|
|
| 100 |
token = choices[0].delta.content
|
| 101 |
|
| 102 |
response += token
|
| 103 |
+
|
| 104 |
+
# Check if this is a game end response and format it nicely
|
| 105 |
+
if "<<" in response and ">>" in response:
|
| 106 |
+
formatted_response = format_game_result(response)
|
| 107 |
+
yield formatted_response
|
| 108 |
+
else:
|
| 109 |
+
yield response
|
| 110 |
|
| 111 |
|
| 112 |
"""
|
|
|
|
| 115 |
chatbot = gr.ChatInterface(
|
| 116 |
respond,
|
| 117 |
type="messages",
|
| 118 |
+
description="I am thinking of a country, you have 20 yes or no questions to ask me to help you figure out what the country is",
|
| 119 |
+
examples=[
|
| 120 |
+
["Is the country located in Europe?"],
|
| 121 |
+
["Is it in the Northern Hemisphere?"],
|
| 122 |
+
["Is the official language Spanish?"],
|
| 123 |
+
["Is the capital city Rome?"],
|
| 124 |
+
["Is this country bordered by an ocean?"],
|
| 125 |
+
["Does this country have more than 100 million people?"],
|
| 126 |
+
["Is this country known for producing coffee?"],
|
| 127 |
+
["Was this country ever a colony of the United Kingdom?"],
|
| 128 |
+
["Is this country located on an island?"],
|
| 129 |
+
["Is the currency the Euro?"],
|
| 130 |
+
],
|
| 131 |
additional_inputs=[
|
| 132 |
+
gr.Textbox(visible=False, value="Geography game placeholder - will be replaced with random country", label="System message"),
|
| 133 |
+
gr.Slider(visible=False, minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 134 |
+
gr.Slider(visible=False, minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 135 |
gr.Slider(
|
| 136 |
+
visible=False,
|
| 137 |
minimum=0.1,
|
| 138 |
maximum=1.0,
|
| 139 |
value=0.95,
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
gradio[oauth]
|