akhaliq HF Staff commited on
Commit
375fb4b
·
verified ·
1 Parent(s): f5da110

Deploy Gradio app with multiple files

Browse files
Files changed (2) hide show
  1. app.py +255 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from openai import OpenAI
4
+ import time
5
+
6
+ # Initialize the OpenAI client with ZenMux
7
+ def get_client(api_key):
8
+ return OpenAI(
9
+ base_url="https://zenmux.ai/api/v1",
10
+ api_key=api_key,
11
+ )
12
+
13
+ def stream_response(messages, api_key, model="inclusionai/ring-1t"):
14
+ """Stream responses from the ZenMux API"""
15
+ try:
16
+ client = get_client(api_key)
17
+
18
+ # Convert messages to the format expected by the API
19
+ api_messages = []
20
+ for msg in messages:
21
+ api_messages.append({
22
+ "role": msg["role"],
23
+ "content": msg["content"]
24
+ })
25
+
26
+ # Create streaming completion
27
+ stream = client.chat.completions.create(
28
+ model=model,
29
+ messages=api_messages,
30
+ stream=True
31
+ )
32
+
33
+ # Accumulate the response
34
+ response_text = ""
35
+ for chunk in stream:
36
+ if chunk.choices[0].delta.content:
37
+ response_text += chunk.choices[0].delta.content
38
+ yield response_text
39
+
40
+ except Exception as e:
41
+ yield f"Error: {str(e)}"
42
+
43
+ def chat_interface(message, history, api_key, model):
44
+ """Handle chat interactions"""
45
+ if not api_key:
46
+ history.append({"role": "assistant", "content": "Please enter your ZenMux API key first."})
47
+ return history
48
+
49
+ # Add user message to history
50
+ history.append({"role": "user", "content": message})
51
+
52
+ # Get assistant response
53
+ full_response = ""
54
+ for response in stream_response(history, api_key, model):
55
+ full_response = response
56
+ # Update the last message (assistant's response)
57
+ if len(history) > 0 and history[-1]["role"] == "assistant":
58
+ history[-1]["content"] = response
59
+ else:
60
+ history.append({"role": "assistant", "content": response})
61
+ yield history
62
+
63
+ # Ensure final response is properly set
64
+ if history and history[-1]["role"] == "assistant":
65
+ history[-1]["content"] = full_response
66
+ else:
67
+ history.append({"role": "assistant", "content": full_response})
68
+ yield history
69
+
70
+ def clear_history():
71
+ """Clear chat history"""
72
+ return []
73
+
74
+ # Custom CSS for a modern look
75
+ css = """
76
+ .gradio-container {
77
+ max-width: 900px !important;
78
+ margin: auto !important;
79
+ }
80
+ .main-header {
81
+ text-align: center;
82
+ margin-bottom: 2rem;
83
+ padding: 1.5rem;
84
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
85
+ border-radius: 15px;
86
+ color: white;
87
+ box-shadow: 0 4px 15px rgba(0,0,0,0.1);
88
+ }
89
+ .main-header h1 {
90
+ margin: 0;
91
+ font-size: 2.5rem;
92
+ font-weight: 700;
93
+ }
94
+ .main-header p {
95
+ margin: 0.5rem 0 0 0;
96
+ opacity: 0.9;
97
+ font-size: 1.1rem;
98
+ }
99
+ .api-section {
100
+ background: #f8f9fa;
101
+ padding: 1.5rem;
102
+ border-radius: 10px;
103
+ margin-bottom: 1.5rem;
104
+ border: 1px solid #e9ecef;
105
+ }
106
+ .chat-container {
107
+ border: 1px solid #e9ecef;
108
+ border-radius: 10px;
109
+ overflow: hidden;
110
+ }
111
+ .message.user {
112
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
113
+ color: white;
114
+ }
115
+ .message.assistant {
116
+ background: #f8f9fa;
117
+ color: #333;
118
+ }
119
+ .message:hover {
120
+ transform: translateY(-1px);
121
+ box-shadow: 0 2px 8px rgba(0,0,0,0.1);
122
+ }
123
+ .footer {
124
+ text-align: center;
125
+ margin-top: 2rem;
126
+ padding: 1rem;
127
+ color: #666;
128
+ font-size: 0.9rem;
129
+ }
130
+ .footer a {
131
+ color: #667eea;
132
+ text-decoration: none;
133
+ font-weight: 600;
134
+ }
135
+ .footer a:hover {
136
+ text-decoration: underline;
137
+ }
138
+ """
139
+
140
+ # Create the Gradio interface
141
+ with gr.Blocks(css=css, title="ZenMux Chat Interface") as demo:
142
+
143
+ # Header
144
+ with gr.Row():
145
+ gr.HTML("""
146
+ <div class="main-header">
147
+ <h1>🤖 ZenMux Chat</h1>
148
+ <p>Powered by InclusionAI Ring-1T • Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: white; text-decoration: underline;">anycoder</a></p>
149
+ </div>
150
+ """)
151
+
152
+ # API Key Section
153
+ with gr.Row():
154
+ with gr.Column():
155
+ gr.HTML("""
156
+ <div class="api-section">
157
+ <h3>🔑 API Configuration</h3>
158
+ <p>Enter your ZenMux API key to start chatting. Get your key from <a href="https://zenmux.ai" target="_blank">zenmux.ai</a></p>
159
+ </div>
160
+ """)
161
+
162
+ api_key_input = gr.Textbox(
163
+ label="ZenMux API Key",
164
+ type="password",
165
+ placeholder="Enter your ZenMux API key here...",
166
+ scale=4
167
+ )
168
+
169
+ model_selector = gr.Dropdown(
170
+ choices=[
171
+ "inclusionai/ring-1t",
172
+ "inclusionai/ring-1t-turbo",
173
+ "inclusionai/ring-1t-lite"
174
+ ],
175
+ value="inclusionai/ring-1t",
176
+ label="Model",
177
+ scale=1
178
+ )
179
+
180
+ # Chat Interface
181
+ with gr.Row():
182
+ chatbot = gr.Chatbot(
183
+ type="messages",
184
+ height=500,
185
+ show_copy_button=True,
186
+ bubble_full_width=False,
187
+ placeholder="Start a conversation by typing a message below..."
188
+ )
189
+
190
+ # Input Section
191
+ with gr.Row():
192
+ msg_input = gr.MultimodalTextbox(
193
+ placeholder="Type your message here...",
194
+ show_label=False,
195
+ scale=4
196
+ )
197
+
198
+ with gr.Column(scale=1):
199
+ submit_btn = gr.Button("Send", variant="primary", size="lg")
200
+ clear_btn = gr.Button("Clear", variant="secondary", size="lg")
201
+
202
+ # Footer
203
+ gr.HTML("""
204
+ <div class="footer">
205
+ <p>Made with ❤️ using Gradio | Powered by <a href="https://zenmux.ai" target="_blank">ZenMux</a></p>
206
+ </div>
207
+ """)
208
+
209
+ # Event handlers
210
+ msg_input.submit(
211
+ chat_interface,
212
+ [msg_input, chatbot, api_key_input, model_selector],
213
+ chatbot
214
+ )
215
+
216
+ submit_btn.click(
217
+ chat_interface,
218
+ [msg_input, chatbot, api_key_input, model_selector],
219
+ chatbot
220
+ )
221
+
222
+ clear_btn.click(
223
+ clear_history,
224
+ outputs=chatbot
225
+ )
226
+
227
+ # Clear input after sending
228
+ def clear_input():
229
+ return ""
230
+
231
+ msg_input.submit(
232
+ clear_input,
233
+ outputs=msg_input
234
+ )
235
+
236
+ submit_btn.click(
237
+ clear_input,
238
+ outputs=msg_input
239
+ )
240
+
241
+ # Examples
242
+ gr.Examples(
243
+ examples=[
244
+ ["What is artificial intelligence?"],
245
+ ["Explain quantum computing in simple terms"],
246
+ ["Write a short story about a robot discovering emotions"],
247
+ ["What are the benefits of renewable energy?"],
248
+ ["Help me understand machine learning concepts"]
249
+ ],
250
+ inputs=msg_input,
251
+ label="Example Prompts"
252
+ )
253
+
254
+ if __name__ == "__main__":
255
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ openai
3
+ python-dotenv