
import aiohttp
import asyncio
import os
import json

async def test_whisper_variant(session, url, file_path, params=None, form_fields=None, description=""):
    print(f"\n--- Testing: {description} ---")
    try:
        data = aiohttp.FormData()
        # Re-open file for each request to ensure pointer is at start
        data.add_field('file', open(file_path, 'rb'), filename='rec.mp3')
        
        if form_fields:
            for k, v in form_fields.items():
                data.add_field(k, str(v))
        
        async with session.post(url, data=data, params=params) as response:
            print(f"Status: {response.status}")
            if response.status == 200:
                try:
                    text_content = await response.text()
                    try:
                        result = json.loads(text_content)
                        # Print keys to see if we got something new
                        if isinstance(result, dict):
                             print("Keys:", result.keys())
                             if 'results' in result and len(result['results']) > 0:
                                 print("Result[0] keys:", result['results'][0].keys())
                             if 'segments' in result:
                                 print("FOUND SEGMENTS!")
                        print("Raw (truncated):", text_content[:300])
                    except:
                        print("Non-JSON Response:", text_content[:300])
                except Exception as e:
                    print(f"Reading error: {e}")
            else:
                print("Error:", await response.text())
    except Exception as e:
        print(f"Exception: {e}")

async def test_whisper():
    url = "http://172.16.31.115:5000/transcribe"
    file_path = "uploads/email_attachments/65d2ca781a6bf3cee9cee0a8ce80acac_rec.mp3"
    
    if not os.path.exists(file_path):
        print(f"File not found: {file_path}")
        return

    async with aiohttp.ClientSession() as session:
        # Variant 1: 'timestamps': 'true' as form field
        await test_whisper_variant(session, url, file_path, form_fields={'timestamps': 'true'}, description="Form: timestamps=true")
        
        # Variant 2: 'response_format': 'verbose_json' (OpenAI style)
        await test_whisper_variant(session, url, file_path, form_fields={'response_format': 'verbose_json'}, description="Form: verbose_json")
        
        # Variant 3: 'verbose': 'true'
        await test_whisper_variant(session, url, file_path, form_fields={'verbose': 'true'}, description="Form: verbose=true")

if __name__ == "__main__":
    asyncio.run(test_whisper())
