File size: 6,511 Bytes
431b26d
 
 
 
 
09aeaac
431b26d
 
 
09aeaac
 
 
 
 
 
 
 
 
 
431b26d
 
 
09aeaac
 
 
431b26d
09aeaac
431b26d
 
09aeaac
 
 
 
 
431b26d
09aeaac
431b26d
 
 
09aeaac
431b26d
09aeaac
431b26d
 
 
09aeaac
 
 
 
 
 
 
 
 
431b26d
 
 
 
 
09aeaac
431b26d
 
 
 
 
 
 
 
 
 
 
09aeaac
431b26d
 
 
 
 
 
09aeaac
431b26d
 
09aeaac
 
 
 
 
431b26d
09aeaac
431b26d
 
 
 
 
 
 
 
 
 
09aeaac
431b26d
 
 
09aeaac
431b26d
09aeaac
431b26d
 
 
09aeaac
 
 
431b26d
 
09aeaac
431b26d
09aeaac
 
 
 
 
 
 
 
 
 
 
431b26d
 
 
 
 
09aeaac
431b26d
09aeaac
 
431b26d
 
 
 
09aeaac
 
431b26d
09aeaac
 
431b26d
09aeaac
431b26d
 
 
 
09aeaac
 
 
 
431b26d
 
09aeaac
431b26d
 
 
09aeaac
 
 
 
431b26d
09aeaac
431b26d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09aeaac
 
 
 
 
 
431b26d
 
 
 
 
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
import gradio as gr
import requests
import os
import time

# Get the token from environment variables
API_TOKEN = os.environ.get("HF_TOKEN", "")
MODEL_NAME = "deepseek-ai/Janus-Pro-7B"
API_URL = f"https://api-inference.huggingface.co/models/{MODEL_NAME}"
headers = {"Authorization": f"Bearer {API_TOKEN}"} if API_TOKEN else {}

def check_token_setup():
    """Check if token is properly set up"""
    if not API_TOKEN:
        return "❌ HF_TOKEN not found. Please add your Hugging Face token in Space Settings β†’ Secrets β†’ HF_TOKEN"
    elif not API_TOKEN.startswith("hf_"):
        return "❌ Invalid token format. Token should start with 'hf_'"
    else:
        return "βœ… Token is properly configured!"

def query_janus_model(payload):
    """Send request to Hugging Face Inference API"""
    if not API_TOKEN:
        return {"error": "No API token configured"}
    
    try:
        response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
        
        if response.status_code == 503:
            return {"error": "πŸ”„ Model is loading, please try again in 30-60 seconds..."}
        elif response.status_code == 401:
            return {"error": "πŸ” Invalid API token. Please check your HF_TOKEN secret."}
        elif response.status_code == 404:
            return {"error": "❌ Model not found. The model might be temporarily unavailable."}
        elif response.status_code != 200:
            return {"error": f"API Error {response.status_code}: {response.text}"}
            
        return response.json()
    except requests.exceptions.Timeout:
        return {"error": "⏰ Request timeout - model might be loading"}
    except Exception as e:
        return {"error": f"πŸ”Œ Connection error: {str(e)}"}

def chat_with_janus(message, history):
    """Chat function for Janus-Pro model"""
    # First check token setup
    token_status = check_token_setup()
    if "❌" in token_status:
        yield token_status
        return
    
    # Show token status
    yield "βœ… Token configured! Processing your request..."
    time.sleep(1)
    
    # Prepare the payload
    payload = {
        "inputs": message,
        "parameters": {
            "max_new_tokens": 350,
            "temperature": 0.7,
            "top_p": 0.9,
            "do_sample": True,
            "return_full_text": False
        },
        "options": {
            "wait_for_model": True
        }
    }
    
    # Show loading message
    yield "πŸ”„ Sending request to Janus-Pro-7B... (First time may take 30-60 seconds)"
    
    # Query the model
    result = query_janus_model(payload)
    
    # Process the response
    if "error" in result:
        yield f"{result['error']}"
    elif isinstance(result, list) and len(result) > 0:
        if 'generated_text' in result[0]:
            generated_text = result[0]['generated_text']
            # Clean up the response
            if generated_text.startswith(message):
                generated_text = generated_text[len(message):].strip()
            yield generated_text
        else:
            yield f"πŸ“„ Response: {str(result[0])}"
    elif isinstance(result, dict) and 'generated_text' in result:
        yield result['generated_text']
    else:
        yield f"πŸ“„ Response: {str(result)}"

def clear_chat():
    """Clear chat history"""
    return [], []

# Create the chat interface
with gr.Blocks(theme=gr.themes.Soft(), title="Janus-Pro-7B API") as demo:
    gr.Markdown(
        """
        # πŸš€ Janus-Pro-7B Chat API
        **Free API for DeepSeek's Multimodal AI Model**
        
        *Understanding & Generation Capabilities*
        """
    )
    
    # Token status display
    token_status = gr.Markdown(check_token_setup())
    
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### πŸ“‹ Instructions")
            gr.Markdown("""
            1. **First message may take 30-60 seconds** (model loading)
            2. **Subsequent messages are faster**
            3. **Be specific** for better responses
            4. Model supports **technical explanations** and **creative writing**
            """)
            
            gr.Markdown("### ℹ️ Model Info")
            gr.Markdown("""
            - **Name**: Janus-Pro-7B
            - **Developer**: DeepSeek AI
            - **Type**: Multimodal (Text + Image)
            - **License**: MIT
            """)
            
        with gr.Column(scale=2):
            chatbot = gr.Chatbot(
                label="πŸ’¬ Chat with Janus-Pro",
                height=400,
                show_copy_button=True,
                placeholder="Your conversation will appear here..."
            )
            
            with gr.Row():
                msg = gr.Textbox(
                    label="Type your message",
                    placeholder="Ask me anything... (Press Enter to send)",
                    scale=4,
                    container=False,
                    autofocus=True
                )
                clear_btn = gr.Button("πŸ—‘οΈ Clear Chat", scale=1)
            
            with gr.Row():
                gr.Examples(
                    examples=[
                        "Explain quantum computing like I'm 10 years old",
                        "Write a short story about a robot learning to paint",
                        "What are the main differences between AI and human intelligence?",
                        "How do transformers work in machine learning?"
                    ],
                    inputs=msg,
                    label="πŸ’‘ Try these examples:"
                )
    
    # Event handlers
    def respond_and_clear(message, history):
        for response in chat_with_janus(message, history):
            yield response
    
    msg.submit(
        fn=respond_and_clear,
        inputs=[msg, chatbot],
        outputs=chatbot
    ).then(
        lambda: "",  # Clear input
        outputs=msg
    )
    
    clear_btn.click(
        fn=clear_chat,
        outputs=[chatbot, msg]
    )
    
    gr.Markdown(
        """
        ---
        **Note**: This is a free API using Hugging Face's Inference API. There might be rate limits for high usage.
        
        **Troubleshooting**:
        - If you get token errors, check your HF_TOKEN secret in Space Settings
        - If model is loading, wait 30-60 seconds and try again
        - For persistent issues, check the Space logs
        """
    )

if __name__ == "__main__":
    demo.launch(debug=True)