C2MV commited on
Commit
d1eb779
·
verified ·
1 Parent(s): 4b90c0d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This is a simple 20 questions-style game built on top of the Anthropic API.
2
+ # Before running this, make sure you have exported your Anthropic API key as an environment variable:
3
+ # export ANTHROPIC_API_KEY="your-anthropic-api-key"
4
+
5
+ import anthropic
6
+ import gradio as gr
7
+
8
+ client = anthropic.Anthropic()
9
+
10
+ def predict(message, history):
11
+ keys_to_keep = ["role", "content"]
12
+ history = [{k: d[k] for k in keys_to_keep if k in d} for d in history]
13
+ history.append({"role": "user", "content": message})
14
+ if len(history) > 20:
15
+ history.append({"role": "user", "content": "DONE"})
16
+ output = client.messages.create(
17
+ messages=history,
18
+ model="claude-3-5-sonnet-20241022",
19
+ max_tokens=1000,
20
+ system="You are guessing an object that the user is thinking of. You can ask 10 yes/no questions. Keep asking questions until the user says DONE"
21
+ )
22
+ return {
23
+ "role": "assistant",
24
+ "content": output.content[0].text,
25
+ "options": [{"value": "Yes"}, {"value": "No"}]
26
+ }
27
+
28
+ placeholder = """
29
+ <center><h1>10 Questions</h1><br>Think of a person, place, or thing. I'll ask you 10 yes/no questions to try and guess it.
30
+ </center>
31
+ """
32
+
33
+ demo = gr.ChatInterface(
34
+ predict,
35
+ examples=["Start!"],
36
+ chatbot=gr.Chatbot(placeholder=placeholder),
37
+ type="messages"
38
+ )
39
+
40
+ demo.launch()