Spaces:
Sleeping
Sleeping
| 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) | |