Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,37 +4,104 @@
|
|
4 |
|
5 |
import anthropic
|
6 |
import gradio as gr
|
|
|
7 |
|
|
|
8 |
client = anthropic.Anthropic()
|
9 |
|
10 |
def predict(message, history):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
"role": "
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
placeholder = """
|
29 |
-
<center
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
</center>
|
31 |
"""
|
32 |
|
|
|
33 |
demo = gr.ChatInterface(
|
34 |
predict,
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
)
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
import anthropic
|
6 |
import gradio as gr
|
7 |
+
import os
|
8 |
|
9 |
+
# Initialize the Anthropic client
|
10 |
client = anthropic.Anthropic()
|
11 |
|
12 |
def predict(message, history):
|
13 |
+
try:
|
14 |
+
# Convert history to the format expected by Anthropic API
|
15 |
+
anthropic_history = []
|
16 |
+
for msg in history:
|
17 |
+
if isinstance(msg, dict):
|
18 |
+
anthropic_history.append(msg)
|
19 |
+
else:
|
20 |
+
# Handle tuple format if it exists
|
21 |
+
role = "user" if msg[0] else "assistant"
|
22 |
+
anthropic_history.append({"role": role, "content": msg[1]})
|
23 |
+
|
24 |
+
# Add the current message
|
25 |
+
anthropic_history.append({"role": "user", "content": message})
|
26 |
+
|
27 |
+
# Count questions asked (only count assistant messages that are questions)
|
28 |
+
question_count = sum(1 for msg in anthropic_history
|
29 |
+
if msg["role"] == "assistant" and "?" in msg["content"])
|
30 |
+
|
31 |
+
# Check if we've reached the limit or user said "DONE"
|
32 |
+
if question_count >= 10 or message.upper() == "DONE":
|
33 |
+
system_prompt = """You are playing a guessing game. The user was thinking of something and you've been asking yes/no questions to guess it. Now either you've used up your 10 questions or the user said DONE. Make your final guess about what they were thinking of, or ask them to reveal the answer if you're not sure."""
|
34 |
+
else:
|
35 |
+
system_prompt = f"""You are playing a guessing game where you try to guess what the user is thinking of (a person, place, or thing). You can ask up to 10 yes/no questions. You have asked {question_count} questions so far. Ask strategic yes/no questions to narrow down the possibilities. Keep your questions concise and clear."""
|
36 |
+
|
37 |
+
# Make API call to Anthropic
|
38 |
+
response = client.messages.create(
|
39 |
+
model="claude-3-5-sonnet-20241022",
|
40 |
+
max_tokens=1000,
|
41 |
+
system=system_prompt,
|
42 |
+
messages=anthropic_history
|
43 |
+
)
|
44 |
+
|
45 |
+
# Return just the text content - Gradio will handle the formatting
|
46 |
+
return response.content[0].text
|
47 |
+
|
48 |
+
except Exception as e:
|
49 |
+
return f"Sorry, I encountered an error: {str(e)}"
|
50 |
+
|
51 |
+
# Custom CSS for better styling
|
52 |
+
custom_css = """
|
53 |
+
#chatbot {
|
54 |
+
height: 500px;
|
55 |
+
}
|
56 |
+
.message {
|
57 |
+
font-size: 16px;
|
58 |
+
}
|
59 |
+
"""
|
60 |
|
61 |
placeholder = """
|
62 |
+
<center>
|
63 |
+
<h1>π― 10 Questions Game</h1>
|
64 |
+
<p>Think of a person, place, or thing. I'll ask you up to 10 yes/no questions to try and guess it!</p>
|
65 |
+
<p><strong>Instructions:</strong></p>
|
66 |
+
<ul style="text-align: left; display: inline-block;">
|
67 |
+
<li>Think of something specific (person, place, or thing)</li>
|
68 |
+
<li>Answer my questions with "Yes" or "No"</li>
|
69 |
+
<li>Type "DONE" if you want me to make my final guess early</li>
|
70 |
+
<li>Have fun! π</li>
|
71 |
+
</ul>
|
72 |
</center>
|
73 |
"""
|
74 |
|
75 |
+
# Create the Gradio interface
|
76 |
demo = gr.ChatInterface(
|
77 |
predict,
|
78 |
+
title="10 Questions Guessing Game",
|
79 |
+
examples=["Let's start!", "I'm ready!", "Begin the game!"],
|
80 |
+
chatbot=gr.Chatbot(
|
81 |
+
placeholder=placeholder,
|
82 |
+
height=500,
|
83 |
+
show_copy_button=True
|
84 |
+
),
|
85 |
+
textbox=gr.Textbox(
|
86 |
+
placeholder="Answer 'Yes' or 'No' to my questions, or type 'DONE' to finish early...",
|
87 |
+
container=False,
|
88 |
+
scale=7
|
89 |
+
),
|
90 |
+
type="messages",
|
91 |
+
css=custom_css,
|
92 |
+
theme=gr.themes.Soft()
|
93 |
)
|
94 |
|
95 |
+
if __name__ == "__main__":
|
96 |
+
# Check if API key is set
|
97 |
+
if not os.getenv("ANTHROPIC_API_KEY"):
|
98 |
+
print("β οΈ Warning: ANTHROPIC_API_KEY environment variable not set!")
|
99 |
+
print("Please set it with: export ANTHROPIC_API_KEY='your-api-key'")
|
100 |
+
|
101 |
+
print("π Starting 10 Questions Game...")
|
102 |
+
demo.launch(
|
103 |
+
server_name="0.0.0.0",
|
104 |
+
server_port=7860,
|
105 |
+
share=False,
|
106 |
+
debug=True
|
107 |
+
)
|