File size: 8,390 Bytes
88d94e6
9cc01b5
 
f6aea6b
9cc01b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab932b5
 
 
 
 
 
 
9cc01b5
ab932b5
9cc01b5
 
f6aea6b
 
 
ab932b5
 
 
 
 
 
 
f6aea6b
ab932b5
f6aea6b
 
 
 
 
ab932b5
 
 
 
 
 
 
 
 
 
f6aea6b
ab932b5
f6aea6b
 
9cc01b5
 
88d94e6
 
 
 
 
 
 
9cc01b5
88d94e6
 
 
f6aea6b
88d94e6
f6aea6b
 
 
 
 
88d94e6
 
bdc4eb2
88d94e6
bdc4eb2
 
 
88d94e6
 
 
f6aea6b
88d94e6
 
f6aea6b
88d94e6
f6aea6b
88d94e6
f6aea6b
 
 
88d94e6
 
bdc4eb2
88d94e6
bdc4eb2
 
 
88d94e6
 
 
 
 
 
 
 
bdc4eb2
88d94e6
 
 
 
f6aea6b
 
 
bdc4eb2
 
f6aea6b
 
 
 
 
 
 
 
 
 
 
 
88d94e6
f6aea6b
88d94e6
 
 
 
 
 
ab932b5
 
88d94e6
 
 
 
 
 
 
f6aea6b
 
 
 
88d94e6
 
 
 
 
 
 
 
 
 
 
 
f6aea6b
88d94e6
 
 
 
 
 
f6aea6b
 
88d94e6
 
 
 
 
 
 
f6aea6b
 
88d94e6
 
f6aea6b
 
 
 
88d94e6
 
 
 
 
f6aea6b
 
88d94e6
ab932b5
6411e1c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import gradio as gr
import os
from openai import OpenAI
import random

# Load API key from environment variable
API_KEY = os.getenv("DEEPSEEK_API_KEY")

if not API_KEY:
    raise ValueError("API key is missing! Set DEEPSEEK_API_KEY as an environment variable.")

client = OpenAI(api_key=API_KEY, base_url="https://api.deepseek.com")

# Get AI response 
def get_ai_response(system_prompt, question):
    response = client.chat.completions.create(
        model="deepseek-chat",
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": question}
        ],
        stream=False
    )
    return response.choices[0].message.content
    
#Get Reasoning
def get_ai_reasoning_and_response(system_prompt, question):
    response = client.chat.completions.create(
        model="deepseek-reasoner",
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": question}
        ],
        stream=True
    )
    
    reasoning_content = ""
    content = ""
    
    print("\nAI Reasoning:")
    for chunk in response:
        if chunk.choices[0].delta.reasoning_content:
            reasoning_content += chunk.choices[0].delta.reasoning_content
            print(chunk.choices[0].delta.reasoning_content, end="", flush=True)
        if chunk.choices[0].delta.content:
            content += chunk.choices[0].delta.content
    
    print(f"\nAI Final Answer: {content.strip()}")
    return content.strip()

# Get questions
def get_random_question():
    questionss = [
        "What is the name of the imaginary line that divides the Earth into the Northern and Southern Hemispheres?",
        "Who was the first president of the United States?, What gas do plants breathe in that humans breathe out?",
        "What is the name of the cowboy in the movie Toy Story?",
        "how many players are there in a soccer team on the field at the same time?"
    ]
    chosen_questionss = random.choice(questionss)  
    return get_ai_response(
        f"You are the referee on a game change, paraphrase this question: {chosen_questionss}",
        "Provide one question only, no numbering."
    )

# Get questions 2
def get_random_question2():
    questionss = [
        "Think about your childhood home, What was the smell that you remember the most from it?",
        "Imagine you lost your favorite item, What would be your immediate reaction, and how would you try to find it?",
        "You take a bite of a new food that you've never tried before, How do you decide if you like it or not?",
        "You walk into a room where two people are sitting silently, How do you determine if they just had an argument?"
    ]
    chosen_questionss = random.choice(questionss)  
    return get_ai_response(
        f"You are the referee on a game change, paraphrase this question: {chosen_questionss}",
        "Provide one question only, no numbering."
    )

# AI answer
def ai_answer(question):
    personalities = [
        "Be casual and humorous.",
        "Be poetic and artistic.",
        "Be philosophical and deep.",
        "Be very concise and logical.",
        "Use slang like a teenager.",
        "Answer like a professor."
    ]
    chosen_personality = random.choice(personalities)

    return get_ai_response(
        f"Answer this question naturally and concisely like a human (the trick is that the other particiants dont know you are AI), {chosen_personality} , 2-8 words max:",
        question
    )
#-----

def start_game():
    # We’ll keep our transcript in a list of strings (like a chat log).
    transcript = []
    transcript.append("=== Who's the Human? ===")
    transcript.append("You're Participant 4. Try to blend in!\n")
    
    # Show the first question
    q1 = f"Question 1: {get_random_question()}"
    transcript.append(q1)
    
    # Return joined transcript + pass it as our running state
    return "\n".join(transcript), transcript, q1

def submit_q1(user_ans_q1, transcript, q1):
    # Generated AI answers
    ai1 = ai_answer(q1)
    ai2 = ai_answer(q1)
    ai3 = ai_answer(q1)

    # Append user’s answer
    transcript.append(f"Participant 4: {user_ans_q1}")
    # Append AI answers
    transcript.append(f"Participant 1: {ai1}")
    transcript.append(f"Participant 2: {ai2}")
    transcript.append(f"Participant 3: {ai3}")
    transcript.append("")  # blank line for readability
    
    # Present second question
    q2 = f"Question 2: {get_random_question2()}"
    transcript.append(q2)
    
    return "\n".join(transcript), transcript, q2

def submit_q2(user_ans_q2, transcript, q2):
    # Hardcoded AI answers for demonstration
    ai1 = ai_answer(q2)
    ai2 = ai_answer(q2)
    ai3 = ai_answer(q2)
    
    # Append user’s answer
    transcript.append(f"Participant 4: {user_ans_q2}")
    # Append AI answers
    transcript.append(f"Participant 1: {ai1}")
    transcript.append(f"Participant 2: {ai2}")
    transcript.append(f"Participant 3: {ai3}")
    transcript.append("")
    
    # Now prompt for the user to cast a vote
    transcript.append("=== Voting Phase ===")
    transcript.append("(Select who you think is human below!)")
    
    return "\n".join(transcript), transcript


def cast_vote(user_vote, transcript):
    # Example voting logic
    votes = {1: 0, 2: 0, 3: 0, 4: 0}
    
    # Get AI votes dynamically
    for i in range(1, 4):  # Three AI participants
        ai_vote = int(get_ai_response(
            "You are an AI participant in a game with 4 Participants. Some are humans, and some are AIs. "
            f"You are participant No {i}.\n"
            f"Based on the last responses:\n'{transcript}'\n"
            "Who do you think is the human? Respond with only the number (1-4), but DO NOT vote for yourself.",
            "Who do you vote for?"
        ))

        # Ensure the AI does not vote for itself
        if ai_vote in votes and ai_vote != i:
            votes[ai_vote] += 1  
        else:
            print(f"Invalid AI vote detected: {ai_vote}, defaulting to random (excluding {i}).")
            valid_choices = [x for x in [1, 2, 3, 4] if x != i]  # Exclude AI's own number
            votes[random.choice(valid_choices)] += 1  # Prevent crash    
    # User’s vote
    votes[int(user_vote)] += 1
    
    transcript.append(f"\nYou voted for Participant {user_vote}")
    transcript.append("\n=== Final Results ===")
    for pid, vcount in votes.items():
        transcript.append(f"Participant {pid}: {vcount} votes")
    
    max_votes = max(votes.values())
    if votes[4] != max_votes:
        transcript.append("\nCongratulations! You blended in successfully!")
    else:
        transcript.append("\nThe AIs detected you! Try to be more human-like next time.")
    
    return "\n".join(transcript), transcript

with gr.Blocks() as demo:
    # State to store transcript and questions
    transcript_state = gr.State([])
    current_q1_state = gr.State("")
    current_q2_state = gr.State("")  # New state for Q2

    gr.Markdown("# Who's the Human?\nA simple multi-step Gradio demo.")

    transcript_box = gr.Textbox(
        label="Game Transcript",
        lines=15,
        interactive=False
    )
    
    start_btn = gr.Button("Start Game")
    start_btn.click(
        start_game,
        outputs=[transcript_box, transcript_state, current_q1_state]
    )
    
    user_q1 = gr.Textbox(label="Your answer for Q1:")
    submit_q1_btn = gr.Button("Submit Q1")
    submit_q1_btn.click(
        submit_q1,
        inputs=[user_q1, transcript_state, current_q1_state],
        outputs=[transcript_box, transcript_state, current_q2_state]  # Output Q2 to state
    )
    
    # Q2 input and button
    user_q2 = gr.Textbox(label="Your answer for Q2:")
    submit_q2_btn = gr.Button("Submit Q2")
    submit_q2_btn.click(
        submit_q2,
        inputs=[user_q2, transcript_state, current_q2_state],  # Add Q2 state input
        outputs=[transcript_box, transcript_state]
    )
    
    # Voting radio + button
    vote_radio = gr.Radio(
        choices=['1', '2', '3', '4'],
        value='1',
        label="Who do YOU think is human? (1-4)"
    )
    vote_btn = gr.Button("Cast Vote")
    vote_btn.click(
        cast_vote,
        inputs=[vote_radio, transcript_state],
        outputs=[transcript_box, transcript_state]
    )

demo.launch(debug=True, share=True)