File size: 14,593 Bytes
e30257d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from flask import Flask, render_template, request, jsonify
from flask_socketio import SocketIO, emit
import os
import requests
import json
import uuid
from datetime import datetime
from dotenv import load_dotenv
import logging
from werkzeug.utils import secure_filename
import random
import asyncio

# Initialize Flask and configure core settings
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)  # Generate a random secret key for security
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # Limit file uploads to 16MB

# Initialize SocketIO with CORS support for development
socketio = SocketIO(app, cors_allowed_origins="*")

# Load environment variables from .env file
load_dotenv()
MISTRAL_API_KEY = os.getenv('MISTRAL_API_KEY')
ELEVENLABS_API_KEY = os.getenv('ELEVENLABS_API_KEY')

# Configure logging to track application behavior
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class GameState:
    """Manages the state of all active game sessions."""
    
    def __init__(self):
        """Initialize the game state manager."""
        self.games = {}  # Dictionary to store all active games
        self.cleanup_interval = 3600  # Cleanup inactive games every hour
        
    def create_game(self):
        """Create a new game session with a unique identifier."""
        game_id = str(uuid.uuid4())
        self.games[game_id] = {
            'players': [],
            'current_phase': 'setup',
            'recordings': {},
            'impostor': None,
            'votes': {},
            'question': None,
            'impostor_answer': None,
            'modified_recording': None,
            'round_number': 1,
            'start_time': datetime.now().isoformat(),
            'completed_rounds': [],
            'score': {'impostor_wins': 0, 'player_wins': 0}
        }
        return game_id

    def cleanup_inactive_games(self):
        """Remove inactive game sessions older than 2 hours."""
        current_time = datetime.now()
        inactive_threshold = 7200  # 2 hours in seconds
        
        for game_id, game in list(self.games.items()):
            start_time = datetime.fromisoformat(game['start_time'])
            if (current_time - start_time).total_seconds() > inactive_threshold:
                del self.games[game_id]

# Initialize global game state
game_state = GameState()

async def generate_question():
    """Generate an engaging question using Mistral AI."""
    try:
        headers = {
            'Authorization': f'Bearer {MISTRAL_API_KEY}',
            'Content-Type': 'application/json'
        }
        
        # Craft a prompt that encourages interesting, personal questions
        payload = {
            'messages': [{
                'role': 'user',
                'content': '''Generate an engaging personal question for a social game.

                The question should:

                1. Encourage creative and unique responses

                2. Be open-ended but not too philosophical

                3. Be answerable in 15-30 seconds

                4. Be appropriate for all ages

                5. Spark interesting conversation



                Generate only the question, without any additional text.'''
            }]
        }
        
        response = requests.post(
            'https://api.mistral.ai/v1/chat/completions',
            headers=headers,
            json=payload,
            timeout=10  # Set timeout to handle slow responses
        )
        
        if response.status_code == 200:
            question = response.json()['choices'][0]['message']['content'].strip()
            logger.info(f"Generated question: {question}")
            return question
        else:
            logger.error(f"Mistral API error: {response.status_code}")
            # Fallback questions if API fails
            fallback_questions = [
                "What is your favorite childhood memory?",
                "What's the most interesting place you've ever visited?",
                "What's a skill you'd love to master and why?",
                "What's the best piece of advice you've ever received?"
            ]
            return random.choice(fallback_questions)
            
    except Exception as e:
        logger.error(f"Error generating question: {str(e)}")
        return "What is your favorite memory from your childhood?"

async def generate_impostor_answer(question):
    """Generate a convincing impostor response using Mistral AI."""
    try:
        headers = {
            'Authorization': f'Bearer {MISTRAL_API_KEY}',
            'Content-Type': 'application/json'
        }
        
        # Craft a detailed prompt for generating a natural response
        prompt = f'''Given the question "{question}", generate a detailed and convincing personal response.

        The response should:

        1. Sound natural and conversational

        2. Be 2-3 sentences long

        3. Include specific details to sound authentic

        4. Be suitable for text-to-speech conversion

        5. Avoid complex words or punctuation that might affect voice synthesis

        

        Generate only the response, without any additional context.'''
        
        payload = {
            'messages': [{
                'role': 'user',
                'content': prompt
            }]
        }
        
        response = requests.post(
            'https://api.mistral.ai/v1/chat/completions',
            headers=headers,
            json=payload,
            timeout=10
        )
        
        if response.status_code == 200:
            answer = response.json()['choices'][0]['message']['content'].strip()
            logger.info(f"Generated impostor answer: {answer}")
            return answer
        else:
            logger.error(f"Mistral API error generating answer: {response.status_code}")
            return "I have an interesting story about that, but I'd need more time to explain it properly."
            
    except Exception as e:
        logger.error(f"Error generating impostor answer: {str(e)}")
        return "I have an interesting story about that, but I'd need more time to explain it properly."

async def clone_voice(audio_file):
    """Clone a voice using ElevenLabs API."""
    try:
        headers = {
            'xi-api-key': ELEVENLABS_API_KEY
        }
        
        with open(audio_file, 'rb') as f:
            files = {
                'files': ('recording.wav', f, 'audio/wav')
            }
            
            response = requests.post(
                'https://api.elevenlabs.io/v1/voices/add',
                headers=headers,
                files=files,
                timeout=30
            )
            
        if response.status_code == 200:
            voice_id = response.json().get('voice_id')
            logger.info(f"Successfully cloned voice: {voice_id}")
            return voice_id
        else:
            logger.error(f"ElevenLabs voice cloning error: {response.status_code}")
            return None
            
    except Exception as e:
        logger.error(f"Error cloning voice: {str(e)}")
        return None

async def generate_cloned_speech(voice_id, text):
    """Generate speech using ElevenLabs with a cloned voice."""
    try:
        headers = {
            'xi-api-key': ELEVENLABS_API_KEY,
            'Content-Type': 'application/json'
        }
        
        payload = {
            'text': text,
            'voice_settings': {
                'stability': 0.75,
                'similarity_boost': 0.75
            }
        }
        
        response = requests.post(
            f'https://api.elevenlabs.io/v1/text-to-speech/{voice_id}',
            headers=headers,
            json=payload,
            timeout=30
        )
        
        if response.status_code == 200:
            filename = f"temp/impostor_audio_{uuid.uuid4()}.mp3"
            with open(filename, 'wb') as f:
                f.write(response.content)
            logger.info(f"Generated cloned speech: {filename}")
            return filename
        else:
            logger.error(f"ElevenLabs speech generation error: {response.status_code}")
            return None
            
    except Exception as e:
        logger.error(f"Error generating cloned speech: {str(e)}")
        return None

@app.route('/')
def home():
    """Serve the main game page."""
    return render_template('index.html')

@app.route('/api/start_game', methods=['POST'])
async def start_game():
    """Initialize a new game session."""
    try:
        data = request.get_json()
        game_id = data.get('game_id')
        
        if game_id not in game_state.games:
            return jsonify({'error': 'Game not found'}), 404
            
        game = game_state.games[game_id]
        
        # Generate question for the round
        question = await generate_question()
        game['question'] = question
        game['current_phase'] = 'recording'
        
        return jsonify({
            'status': 'success',
            'question': question
        })
        
    except Exception as e:
        logger.error(f"Error starting game: {str(e)}")
        return jsonify({'error': 'Internal server error'}), 500

@app.route('/api/submit_recording', methods=['POST'])
async def submit_recording():
    """Handle voice recording submissions."""
    try:
        game_id = request.form.get('game_id')
        player_id = request.form.get('player_id')
        audio_file = request.files.get('audio')
        
        if not all([game_id, player_id, audio_file]):
            return jsonify({'error': 'Missing required data'}), 400
            
        if game_id not in game_state.games:
            return jsonify({'error': 'Game not found'}), 404
            
        # Save the recording
        filename = secure_filename(f"recording_{game_id}_{player_id}.wav")
        filepath = os.path.join('temp', filename)
        audio_file.save(filepath)
        
        game_state.games[game_id]['recordings'][player_id] = filepath
        
        return jsonify({'status': 'success'})
        
    except Exception as e:
        logger.error(f"Error submitting recording: {str(e)}")
        return jsonify({'error': 'Internal server error'}), 500

@app.route('/api/process_impostor', methods=['POST'])
async def process_impostor():
    """Handle the complete impostor voice generation process."""
    try:
        data = request.get_json()
        game_id = data.get('game_id')
        impostor_id = data.get('impostor_id')
        
        if game_id not in game_state.games:
            return jsonify({'error': 'Game not found'}), 404
            
        game = game_state.games[game_id]
        
        # Generate impostor's answer
        impostor_answer = await generate_impostor_answer(game['question'])
        game['impostor_answer'] = impostor_answer
        
        # Get impostor's original recording
        original_recording = game['recordings'].get(impostor_id)
        if not original_recording:
            return jsonify({'error': 'Original recording not found'}), 404
        
        # Clone voice
        voice_id = await clone_voice(original_recording)
        if not voice_id:
            return jsonify({'error': 'Voice cloning failed'}), 500
            
        # Generate modified speech
        audio_file = await generate_cloned_speech(voice_id, impostor_answer)
        if not audio_file:
            return jsonify({'error': 'Speech generation failed'}), 500
            
        game['modified_recording'] = audio_file
        
        return jsonify({
            'status': 'success',
            'audio_url': audio_file
        })
        
    except Exception as e:
        logger.error(f"Error processing impostor: {str(e)}")
        return jsonify({'error': 'Internal server error'}), 500

@socketio.on('join_game')
def handle_join_game(data):
    """Handle a player joining the game."""
    game_id = data.get('game_id')
    player_name = data.get('player_name')
    
    if game_id in game_state.games:
        game = game_state.games[game_id]
        if len(game['players']) < 5:
            player_id = len(game['players']) + 1
            game['players'].append({
                'id': player_id,
                'name': player_name
            })
            emit('player_joined', {
                'player_id': player_id,
                'player_name': player_name
            }, broadcast=True, room=game_id)

@socketio.on('submit_vote')
def handle_vote(data):
    """Process player votes and determine round outcome."""
    game_id = data.get('game_id')
    voter_id = data.get('voter_id')
    vote_for = data.get('vote_for')
    
    if game_id in game_state.games:
        game = game_state.games[game_id]
        game['votes'][voter_id] = vote_for
        
        # Check if all players have voted
        if len(game['votes']) == len(game['players']):
            # Calculate results
            votes_count = {}
            for vote in game['votes'].values():
                votes_count[vote] = votes_count.get(vote, 0) + 1
            
            most_voted = max(votes_count.items(), key=lambda x: x[1])[0]
            
            # Update scores
            if most_voted == game['impostor']:
                game['score']['player_wins'] += 1
            else:
                game['score']['impostor_wins'] += 1
            
            # Store round results
            game['completed_rounds'].append({
                'round_number': game['round_number'],
                'impostor': game['impostor'],
                'votes': game['votes'].copy(),
                'most_voted': most_voted
            })
            
            # Emit results
            emit('round_result', {
                'impostor': game['impostor'],
                'most_voted': most_voted,
                'votes': game['votes'],
                'score': game['score']
            }, broadcast=True, room=game_id)

if __name__ == '__main__':
    # Create temporary directory for recordings if it doesn't exist
    os.makedirs('temp', exist_ok=True)
    
    # Start the server
    socketio.run(app, host='0.0.0.0', port=7860, debug=True)