File size: 4,345 Bytes
4047fda
 
 
e3b6b7d
 
4047fda
e3b6b7d
 
4047fda
e3b6b7d
 
 
4047fda
e3b6b7d
 
 
 
 
 
 
 
 
 
 
 
 
65bcfb8
4047fda
22eb0d1
 
3543add
e3b6b7d
 
 
 
3543add
65bcfb8
3543add
137ecd9
 
 
 
 
 
3543add
4047fda
e3b6b7d
22eb0d1
e3b6b7d
 
 
 
 
3543add
 
 
e3b6b7d
 
65bcfb8
137ecd9
 
65bcfb8
e3b6b7d
 
 
3543add
e3b6b7d
 
e9fb849
e3b6b7d
 
3543add
e3b6b7d
 
e9fb849
e3b6b7d
 
 
 
 
4047fda
e3b6b7d
 
4047fda
 
e3b6b7d
65bcfb8
 
 
3543add
e3b6b7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65bcfb8
e3b6b7d
 
 
 
 
 
e9fb849
52a1685
e3b6b7d
 
 
 
65bcfb8
52a1685
 
 
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
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()