import gradio as gr import os # Get token HF_TOKEN = os.getenv("HF_TOKEN") def simple_test(user_text): """Super simple test function with detailed debugging""" # Step 1: Check if we got input if not user_text or user_text.strip() == "": return "❌ No input provided" # Step 2: Check token exists if not HF_TOKEN: return "❌ ERROR: HF_TOKEN not found in environment" # Step 3: Show token prefix (first 10 chars only - safe to show) token_preview = HF_TOKEN[:10] + "..." if len(HF_TOKEN) > 10 else "Token too short!" # Step 4: Try importing requests try: import requests except ImportError: return "❌ ERROR: 'requests' library not installed" # Step 5: Try calling the API with NEW endpoint try: # CORRECT ENDPOINT - Using router.huggingface.co API_URL = "https://router.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2/v1/chat/completions" headers = { "Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json" } # NEW FORMAT - Chat completions style payload = { "model": "mistralai/Mistral-7B-Instruct-v0.2", "messages": [ {"role": "user", "content": "Say hello in a friendly way"} ], "max_tokens": 50, "temperature": 0.7 } # Show we're about to make the call status_msg = f"✅ Token found: {token_preview}\n✅ Using ROUTER endpoint (router.huggingface.co)\n✅ Calling API...\n\n" response = requests.post(API_URL, headers=headers, json=payload, timeout=30) # Show response details status_msg += f"Response Code: {response.status_code}\n\n" if response.status_code == 200: result = response.json() status_msg += f"✅ SUCCESS!\n\nAPI Response:\n{result}" # Extract text from new format if 'choices' in result and len(result['choices']) > 0: text = result['choices'][0].get('message', {}).get('content', 'No text found') status_msg += f"\n\n✅ Generated Text:\n{text}" return status_msg elif response.status_code == 503: return status_msg + "⏳ Model is loading. Wait 30-60 seconds and try again." elif response.status_code == 401 or response.status_code == 403: return status_msg + f"❌ Authentication failed!\n\nYour token might be invalid or expired.\n\nResponse:\n{response.text}" else: return status_msg + f"❌ Unexpected error\n\nResponse:\n{response.text[:500]}" except requests.exceptions.Timeout: return "⏳ Request timed out (took more than 30 seconds)" except requests.exceptions.RequestException as e: return f"❌ Network error:\n{str(e)}" except Exception as e: import traceback return f"❌ Unexpected error:\n{str(e)}\n\nFull traceback:\n{traceback.format_exc()}" # Simple interface with gr.Blocks(title="DEBUG MODE - FIXED", theme=gr.themes.Soft()) as demo: gr.Markdown("# 🔧 DEBUG MODE - VYZ Assistant (NEW ENDPOINT)") gr.Markdown("## Using the NEW Hugging Face API endpoint") if HF_TOKEN: gr.Markdown(f"**Token Status:** ✅ Found (starts with `{HF_TOKEN[:7]}...`)") else: gr.Markdown("**Token Status:** ❌ NOT FOUND") gr.Markdown("---") input_box = gr.Textbox( label="Type anything (like 'hello')", placeholder="hello", lines=2 ) output_box = gr.Textbox( label="Detailed Debug Output", lines=15, interactive=False ) test_btn = gr.Button("🧪 Test API Call (NEW ENDPOINT)", variant="primary", size="lg") test_btn.click( fn=simple_test, inputs=input_box, outputs=output_box ) gr.Markdown("---") gr.Markdown("### Instructions:") gr.Markdown("1. Type 'hello' in the box above") gr.Markdown("2. Click the 'Test API Call' button") gr.Markdown("3. Wait for the output") gr.Markdown("4. Tell me what it says!") if __name__ == "__main__": demo.launch()