mohitk1234 commited on
Commit
6272c32
·
verified ·
1 Parent(s): 030ff18

Newversion1

Browse files
Files changed (1) hide show
  1. app.py +348 -0
app.py ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import openai
3
+ from openai import OpenAI
4
+ import os
5
+ from typing import List, Tuple
6
+
7
+ # Initialize OpenAI with environment variable
8
+ # Set OPENAI_API_KEY in Hugging Face secrets
9
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
10
+
11
+ # Global session state
12
+ chat_history = []
13
+
14
+ def respond_to_message(message: str, history: List[List[str]]) -> Tuple[str, List[List[str]]]:
15
+ """Handle user message and return response with updated history"""
16
+ if not message.strip():
17
+ return "", history
18
+
19
+ # Add user message to history
20
+ history.append([message, ""])
21
+
22
+ # Add to global chat history for sidebar
23
+ global chat_history
24
+ chat_history.append(message)
25
+
26
+ try:
27
+ # Prepare messages for OpenAI API
28
+ messages = [{"role": "system", "content": "You are a helpful assistant."}]
29
+
30
+ # Add conversation history
31
+ for user_msg, assistant_msg in history[:-1]: # Exclude the current message
32
+ if user_msg and assistant_msg:
33
+ messages.append({"role": "user", "content": user_msg})
34
+ messages.append({"role": "assistant", "content": assistant_msg})
35
+
36
+ # Add current user message
37
+ messages.append({"role": "user", "content": message})
38
+
39
+ # Get response from OpenAI
40
+ response = client.chat.completions.create(
41
+ model="gpt-3.5-turbo", # You can change to gpt-4 if available
42
+ messages=messages,
43
+ max_tokens=1000,
44
+ temperature=0.7
45
+ )
46
+
47
+ assistant_response = response.choices[0].message.content
48
+
49
+ # Update the last message in history with assistant response
50
+ history[-1][1] = assistant_response
51
+
52
+ return "", history
53
+
54
+ except Exception as e:
55
+ error_message = f"Error: {str(e)}"
56
+ history[-1][1] = error_message
57
+ return "", history
58
+
59
+ def clear_chat() -> Tuple[str, List[List[str]]]:
60
+ """Clear the current chat history"""
61
+ global chat_history
62
+ chat_history = []
63
+ return "", []
64
+
65
+ def get_chat_history() -> List[str]:
66
+ """Get chat history for sidebar"""
67
+ return chat_history
68
+
69
+ # Custom CSS for ChatGPT-like styling
70
+ custom_css = """
71
+ .gradio-container {
72
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
73
+ background: white !important;
74
+ min-height: 100vh;
75
+ }
76
+
77
+ .chat-layout {
78
+ display: flex;
79
+ height: 100vh;
80
+ max-width: 100%;
81
+ margin: 0 auto;
82
+ background: white;
83
+ }
84
+
85
+ .sidebar {
86
+ width: 280px;
87
+ background: #f7f7f8;
88
+ color: #374151;
89
+ padding: 0;
90
+ display: flex;
91
+ flex-direction: column;
92
+ border-right: 1px solid #e5e7eb;
93
+ overflow-y: auto;
94
+ }
95
+
96
+ .sidebar-header {
97
+ padding: 16px;
98
+ border-bottom: 1px solid #e5e7eb;
99
+ background: white;
100
+ }
101
+
102
+ .history-list {
103
+ flex: 1;
104
+ padding: 8px;
105
+ background: #f7f7f8;
106
+ overflow-y: auto;
107
+ }
108
+
109
+ .history-item {
110
+ background: transparent !important;
111
+ color: #374151 !important;
112
+ border: none !important;
113
+ border-radius: 6px !important;
114
+ padding: 8px 12px !important;
115
+ font-size: 13px !important;
116
+ cursor: pointer !important;
117
+ transition: background-color 0.2s !important;
118
+ width: 100% !important;
119
+ text-align: left !important;
120
+ margin-bottom: 4px !important;
121
+ box-shadow: none !important;
122
+ line-height: 1.4 !important;
123
+ word-wrap: break-word !important;
124
+ white-space: normal !important;
125
+ }
126
+
127
+ .history-item:hover {
128
+ background: #e5e7eb !important;
129
+ }
130
+
131
+ .history-item.active {
132
+ background: #e5e7eb !important;
133
+ }
134
+
135
+ .main-chat {
136
+ flex: 1;
137
+ display: flex;
138
+ flex-direction: column;
139
+ background: white;
140
+ min-height: 0;
141
+ }
142
+
143
+ .chat-header {
144
+ padding: 12px 24px;
145
+ border-bottom: 1px solid #e5e7eb;
146
+ color: #374151;
147
+ font-size: 16px;
148
+ font-weight: 600;
149
+ background: white;
150
+ }
151
+
152
+ .chat-messages {
153
+ flex: 1;
154
+ overflow-y: auto;
155
+ padding: 0;
156
+ background: white;
157
+ min-height: 0;
158
+ }
159
+
160
+ .chatbot {
161
+ background: transparent !important;
162
+ border: none !important;
163
+ box-shadow: none !important;
164
+ padding: 0 !important;
165
+ height: 100% !important;
166
+ }
167
+
168
+ .chatbot .message {
169
+ margin: 0 !important;
170
+ padding: 16px 0 !important;
171
+ border-radius: 0 !important;
172
+ border-bottom: 1px solid #e5e7eb !important;
173
+ }
174
+
175
+ .chatbot .user-message {
176
+ background: white !important;
177
+ color: #374151 !important;
178
+ margin: 0 !important;
179
+ padding: 16px 24px !important;
180
+ }
181
+
182
+ .chatbot .bot-message {
183
+ background: #f9fafb !important;
184
+ color: #374151 !important;
185
+ margin: 0 !important;
186
+ padding: 16px 24px !important;
187
+ }
188
+
189
+ .chat-input-container {
190
+ padding: 16px 24px;
191
+ border-top: 1px solid #e5e7eb;
192
+ background: white;
193
+ position: sticky;
194
+ bottom: 0;
195
+ }
196
+
197
+ .chat-input {
198
+ background: white !important;
199
+ border: 1px solid #d1d5db !important;
200
+ color: #374151 !important;
201
+ border-radius: 8px !important;
202
+ padding: 12px 16px !important;
203
+ font-size: 16px !important;
204
+ resize: none !important;
205
+ min-height: 48px !important;
206
+ }
207
+
208
+ .chat-input:focus {
209
+ border-color: #3b82f6 !important;
210
+ box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2) !important;
211
+ }
212
+
213
+ .send-button {
214
+ background: #3b82f6 !important;
215
+ color: white !important;
216
+ border: none !important;
217
+ border-radius: 6px !important;
218
+ padding: 8px 16px !important;
219
+ font-size: 14px !important;
220
+ font-weight: 500 !important;
221
+ cursor: pointer !important;
222
+ transition: background-color 0.2s !important;
223
+ height: 36px !important;
224
+ }
225
+
226
+ .send-button:hover {
227
+ background: #2563eb !important;
228
+ }
229
+
230
+ .clear-button {
231
+ background: #6b7280 !important;
232
+ color: white !important;
233
+ border: none !important;
234
+ border-radius: 6px !important;
235
+ padding: 8px 16px !important;
236
+ font-size: 14px !important;
237
+ cursor: pointer !important;
238
+ transition: background-color 0.2s !important;
239
+ height: 36px !important;
240
+ }
241
+
242
+ .clear-button:hover {
243
+ background: #4b5563 !important;
244
+ }
245
+
246
+ @media (max-width: 768px) {
247
+ .chat-layout {
248
+ flex-direction: column;
249
+ }
250
+
251
+ .sidebar {
252
+ width: 100%;
253
+ height: auto;
254
+ max-height: 200px;
255
+ }
256
+
257
+ .main-chat {
258
+ flex: 1;
259
+ }
260
+ }
261
+ """
262
+
263
+ # Create the Gradio interface
264
+ with gr.Blocks(css=custom_css, title="ChatGPT Clone") as demo:
265
+
266
+ with gr.Row(elem_classes="chat-layout"):
267
+ # Left sidebar with chat history
268
+ with gr.Column(elem_classes="sidebar"):
269
+ # Sidebar header
270
+ with gr.Row(elem_classes="sidebar-header"):
271
+ gr.Markdown("### 💬 Chat History")
272
+
273
+ # Chat history list
274
+ with gr.Column(elem_classes="history-list"):
275
+ chat_history_display = gr.Textbox(
276
+ value="",
277
+ label="",
278
+ elem_classes="history-item",
279
+ show_label=False,
280
+ interactive=False,
281
+ lines=25
282
+ )
283
+
284
+ # Main chat area
285
+ with gr.Column(elem_classes="main-chat"):
286
+ # Chat header
287
+ with gr.Row(elem_classes="chat-header"):
288
+ gr.Markdown("### AI Assistant")
289
+
290
+ # Chat messages
291
+ chatbot = gr.Chatbot(
292
+ elem_classes="chatbot",
293
+ height=500,
294
+ show_label=False,
295
+ container=True,
296
+ bubble_full_width=True
297
+ )
298
+
299
+ # Chat input area
300
+ with gr.Row(elem_classes="chat-input-container"):
301
+ with gr.Column(scale=4):
302
+ user_input = gr.Textbox(
303
+ placeholder="Type your message here...",
304
+ show_label=False,
305
+ lines=2,
306
+ max_lines=4,
307
+ elem_classes="chat-input"
308
+ )
309
+ with gr.Column(scale=1):
310
+ send_btn = gr.Button("Send", elem_classes="send-button")
311
+ clear_btn = gr.Button("Clear", elem_classes="clear-button")
312
+
313
+ # Event handlers
314
+ def handle_send(message, history):
315
+ result = respond_to_message(message, history)
316
+ # Update chat history display as list items
317
+ if chat_history:
318
+ history_text = ""
319
+ for i, msg in enumerate(chat_history):
320
+ history_text += f"• {msg}\n"
321
+ else:
322
+ history_text = "No chat history yet"
323
+ return result[0], result[1], history_text
324
+
325
+ def handle_clear():
326
+ result = clear_chat()
327
+ return result[0], result[1], "No chat history yet"
328
+
329
+ # Connect events
330
+ send_btn.click(
331
+ fn=handle_send,
332
+ inputs=[user_input, chatbot],
333
+ outputs=[user_input, chatbot, chat_history_display]
334
+ )
335
+
336
+ user_input.submit(
337
+ fn=handle_send,
338
+ inputs=[user_input, chatbot],
339
+ outputs=[user_input, chatbot, chat_history_display]
340
+ )
341
+
342
+ clear_btn.click(
343
+ fn=handle_clear,
344
+ outputs=[user_input, chatbot, chat_history_display]
345
+ )
346
+
347
+ # Launch the app for Hugging Face
348
+ demo.launch()