Chaty12 commited on
Commit
6fb4e78
verified
1 Parent(s): 04c49bc

Upload chaty_ai_original.py

Browse files
Files changed (1) hide show
  1. chaty_ai_original.py +661 -0
chaty_ai_original.py ADDED
@@ -0,0 +1,661 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Chaty AI original.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1V1aACjqfyQoyVUSHPpKT3vEYS49J7l-8
8
+ """
9
+
10
+ pip install diffusers transformers torch accelerate cohere langchain gradio
11
+
12
+ import os
13
+ from google.colab import userdata
14
+
15
+ # Enable notebook access to secrets
16
+ # This line ensures that the notebook can access secrets stored in Colab's secret manager.
17
+ # No output expected, but this is a necessary setup step.
18
+
19
+ # Retrieve the COHERE_API_KEY from Colab secrets
20
+ COHERE_API_KEY = userdata.get('COHERE_API_KEY')
21
+
22
+ # Set the environment variable for Cohere
23
+ os.environ["COHERE_API_KEY"] = COHERE_API_KEY
24
+
25
+ print("COHERE_API_KEY loaded successfully.")
26
+
27
+ import torch
28
+ from diffusers import StableDiffusionPipeline
29
+
30
+ # 1. Specify the model ID for Stable Diffusion
31
+ model_id = "runwayml/stable-diffusion-v1-5"
32
+
33
+ # 2. Load the pre-trained Stable Diffusion model
34
+ # Check if CUDA is available and set the device accordingly
35
+ device = "cuda" if torch.cuda.is_available() else "cpu"
36
+ pipeline = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
37
+ pipeline.to(device)
38
+
39
+ print(f"Stable Diffusion model '{model_id}' loaded successfully on {device}.")
40
+
41
+ # 3. Define a sample text prompt for image generation
42
+ prompt = "a photo of an astronaut riding a horse on mars"
43
+
44
+ # 4. Generate an image using the loaded pipeline and the prompt
45
+ print(f"Generating image for prompt: '{prompt}'...")
46
+ image = pipeline(prompt).images[0]
47
+
48
+ # 5. Display the generated image
49
+ print("Image generated successfully.")
50
+ image
51
+
52
+ !pip install langchain-cohere
53
+ print("langchain-cohere installed successfully.")
54
+
55
+ from langchain_cohere import ChatCohere
56
+ from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate
57
+ from langchain_core.messages import HumanMessage, AIMessage
58
+
59
+ print("Cohere and Langchain components imported successfully.")
60
+
61
+ import os
62
+ from google.colab import userdata
63
+
64
+ # Retrieve the COHERE_API_KEY from Colab secrets
65
+ COHERE_API_KEY = userdata.get('COHERE_API_KEY')
66
+
67
+ # Set the environment variable for Cohere
68
+ os.environ["COHERE_API_KEY"] = COHERE_API_KEY
69
+
70
+ from langchain_cohere import ChatCohere
71
+
72
+ llm = ChatCohere(cohere_api_key=COHERE_API_KEY)
73
+
74
+ print("Cohere LLM initialized successfully.")
75
+
76
+ system_message_template = SystemMessagePromptTemplate.from_template(
77
+ "You are a helpful AI assistant that can generate creative images based on user descriptions. "
78
+ "You can also answer questions and engage in conversation. "
79
+ "If a user asks for an image, extract the key elements for a Stable Diffusion prompt."
80
+ )
81
+
82
+ human_message_template = HumanMessagePromptTemplate.from_template("User: {user_input}")
83
+
84
+ chat_prompt_template = ChatPromptTemplate.from_messages([
85
+ system_message_template,
86
+ human_message_template
87
+ ])
88
+
89
+ print("ChatPromptTemplate created successfully.")
90
+
91
+ sample_question = "What kind of images can you generate?"
92
+
93
+ # Create a list of messages using the chat_prompt_template
94
+ formatted_messages = chat_prompt_template.format_messages(user_input=sample_question)
95
+
96
+ # Invoke the LLM with the formatted messages
97
+ response = llm.invoke(formatted_messages)
98
+
99
+ print(f"AI Response: {response.content}")
100
+
101
+ system_message_template = SystemMessagePromptTemplate.from_template(
102
+ "You are a helpful AI assistant that can generate creative images based on user descriptions. "
103
+ "You can also answer questions and engage in conversation. "
104
+ "If a user asks for an image, extract the key elements for a Stable Diffusion prompt and output it in the format 'IMAGE_PROMPT: <your image prompt here>'. "
105
+ "Otherwise, provide a normal conversational response."
106
+ )
107
+
108
+ human_message_template = HumanMessagePromptTemplate.from_template("User: {user_input}")
109
+
110
+ chat_prompt_template = ChatPromptTemplate.from_messages([
111
+ system_message_template,
112
+ human_message_template
113
+ ])
114
+
115
+ print("ChatPromptTemplate updated successfully with image generation instruction.")
116
+
117
+ def handle_user_input(user_message: str):
118
+ # 2. Format the user_message using the chat_prompt_template
119
+ formatted_messages = chat_prompt_template.format_messages(user_input=user_message)
120
+
121
+ # 3. Invoke the LLM with the formatted messages
122
+ ai_response = llm.invoke(formatted_messages)
123
+ response_content = ai_response.content
124
+
125
+ generated_image = None
126
+
127
+ # 4. Implement logic to determine if an image generation is implied
128
+ IMAGE_PROMPT_PREFIX = "IMAGE_PROMPT: "
129
+ if response_content.startswith(IMAGE_PROMPT_PREFIX):
130
+ # 5. Extract the actual image description
131
+ image_description = response_content[len(IMAGE_PROMPT_PREFIX):].strip()
132
+ print(f"AI detected an image request. Generating image for: '{image_description}'")
133
+ try:
134
+ # 6. Use the pipeline to generate an image
135
+ generated_image = pipeline(image_description).images[0]
136
+ full_response = f"Image generated successfully based on your request: '{image_description}'"
137
+ except Exception as e:
138
+ full_response = f"Could not generate image for '{image_description}'. Error: {e}"
139
+ generated_image = None
140
+ else:
141
+ # Otherwise, it's a normal conversational response
142
+ full_response = response_content
143
+
144
+ # 7. Return both the AI's full textual response and the generated image object
145
+ return full_response, generated_image
146
+
147
+ # 8. Test the handle_user_input function with a sample conversational input
148
+ conversational_input = "Tell me a fun fact about AI."
149
+ print(f"\nUser: {conversational_input}")
150
+ ai_text_response, generated_image_output = handle_user_input(conversational_input)
151
+ print(f"AI: {ai_text_response}")
152
+ if generated_image_output:
153
+ print("No image expected for this conversational input.")
154
+
155
+ import torch
156
+ from diffusers import StableDiffusionPipeline
157
+
158
+ # 1. Specify the model ID for Stable Diffusion
159
+ model_id = "runwayml/stable-diffusion-v1-5"
160
+
161
+ # 2. Load the pre-trained Stable Diffusion model
162
+ # Check if CUDA is available and set the device accordingly
163
+ device = "cuda" if torch.cuda.is_available() else "cpu"
164
+ pipeline = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
165
+ pipeline.to(device)
166
+
167
+ print(f"Stable Diffusion model '{model_id}' loaded successfully on {device}.")
168
+
169
+ image_request_input = "Generate an image of a futuristic city at sunset with flying cars."
170
+ print(f"\nUser: {image_request_input}")
171
+ ai_text_response, generated_image_output = handle_user_input(image_request_input)
172
+ print(f"AI: {ai_text_response}")
173
+
174
+ if generated_image_output:
175
+ print("Displaying generated image:")
176
+ display(generated_image_output)
177
+ else:
178
+ print("No image was generated for this request.")
179
+
180
+ import gradio as gr
181
+
182
+ print("Gradio library imported successfully.")
183
+
184
+ def gradio_interface_fn(user_query: str):
185
+ ai_text_response, generated_image_output = handle_user_input(user_query)
186
+ print(f"DEBUG: Type of generated_image_output: {type(generated_image_output)}")
187
+ print(f"DEBUG: Is generated_image_output None?: {generated_image_output is None}")
188
+ if generated_image_output is not None:
189
+ print(f"DEBUG: Generated image mode: {generated_image_output.mode}, size: {generated_image_output.size}")
190
+ return ai_text_response, generated_image_output
191
+
192
+ print("Gradio interface function 'gradio_interface_fn' defined.")
193
+
194
+ import base64
195
+ import os
196
+
197
+ image_filename = "Chaty (1).png"
198
+ image_path_in_colab = image_filename
199
+
200
+ base64_image_tag = ""
201
+ favicon_data_uri = "" # New variable to store the base64 data URI for the favicon
202
+
203
+ if os.path.exists(image_path_in_colab):
204
+ try:
205
+ with open(image_path_in_colab, "rb") as image_file:
206
+ encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
207
+ base64_image_tag = f"<img src='data:image/png;base64,{encoded_string}' alt='Chaty Logo' style='height:100px;'>"
208
+ favicon_data_uri = f"data:image/png;base64,{encoded_string}" # Populate the favicon data URI
209
+ print("Image encoded to Base64 successfully and data URI for favicon set.")
210
+ except Exception as e:
211
+ base64_image_tag = ""
212
+ favicon_data_uri = ""
213
+ print(f"An error occurred while encoding the image: {e}")
214
+ else:
215
+ base64_image_tag = ""
216
+ favicon_data_uri = ""
217
+ print(f"Warning: Image file '{image_filename}' not found. Please upload this file to your Colab environment if you wish to display the logo and favicon.")
218
+
219
+ !pip install gTTS
220
+ print("gTTS installed successfully.")
221
+
222
+ chat_history = []
223
+ print("Empty chat history list 'chat_history' initialized.")
224
+
225
+ from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
226
+ from gtts import gTTS
227
+ import os
228
+
229
+ def handle_user_input(user_message: str, chat_history: list):
230
+ # Create the current human message
231
+ current_human_message = HumanMessage(content=user_message)
232
+
233
+ # Get the system message content from the existing template
234
+ system_message_content = system_message_template.format().content
235
+ system_message = SystemMessage(content=system_message_content)
236
+
237
+ # Combine system message, chat history, and current human message for the LLM call
238
+ messages_for_llm = [system_message] + chat_history + [current_human_message]
239
+
240
+ # Invoke the LLM with the combined messages
241
+ ai_response = llm.invoke(messages_for_llm)
242
+ response_content = ai_response.content
243
+
244
+ generated_image = None
245
+ full_response = "" # Initialize full_response
246
+ audio_output_path = None # Initialize audio output path
247
+
248
+ # Implement logic to determine if an image generation is implied
249
+ IMAGE_PROMPT_PREFIX = "IMAGE_PROMPT: "
250
+ if response_content.startswith(IMAGE_PROMPT_PREFIX):
251
+ # Extract the actual image description
252
+ image_description = response_content[len(IMAGE_PROMPT_PREFIX):].strip()
253
+ print(f"AI detected an image request. Generating image for: '{image_description}'")
254
+ try:
255
+ # Use the pipeline to generate an image
256
+ generated_image = pipeline(image_description).images[0]
257
+ full_response = f"Image generated successfully based on your request: '{image_description}'"
258
+ except Exception as e:
259
+ full_response = f"Could not generate image for '{image_description}'. Error: {e}"
260
+ generated_image = None
261
+ else:
262
+ # Otherwise, it's a normal conversational response
263
+ full_response = response_content
264
+
265
+ # Generate speech for the full_response
266
+ try:
267
+ tts = gTTS(text=full_response, lang='es') # Assuming Spanish for the voice output
268
+ audio_file_name = "ai_response.mp3"
269
+ tts.save(audio_file_name)
270
+ audio_output_path = audio_file_name
271
+ print(f"Audio generated and saved to {audio_output_path}")
272
+ except Exception as e:
273
+ print(f"Error generating audio: {e}")
274
+ audio_output_path = None
275
+
276
+ # Update chat history with the current user message and the AI's raw response
277
+ chat_history.append(current_human_message)
278
+ chat_history.append(AIMessage(content=ai_response.content))
279
+
280
+ # Return the AI's full textual response, the generated image object, the audio path, and the updated chat history
281
+ return full_response, generated_image, audio_output_path, chat_history
282
+
283
+ print("handle_user_input function updated to include chat history and speech generation.")
284
+
285
+ from langchain_core.prompts import MessagesPlaceholder
286
+
287
+ # Re-define the system message template (it was already defined but ensures it's current)
288
+ system_message_template = SystemMessagePromptTemplate.from_template(
289
+ "You are a helpful AI assistant that can generate creative images based on user descriptions. "
290
+ "You can also answer questions and engage in conversation. "
291
+ "If a user asks for an image, extract the key elements for a Stable Diffusion prompt and output it in the format 'IMAGE_PROMPT: <your image prompt here>'. "
292
+ "Otherwise, provide a normal conversational response."
293
+ )
294
+
295
+ # Re-define the human message template
296
+ human_message_template = HumanMessagePromptTemplate.from_template("User: {user_input}")
297
+
298
+ # Re-define the chat_prompt_template to include MessagesPlaceholder for chat history
299
+ chat_prompt_template = ChatPromptTemplate.from_messages([
300
+ system_message_template,
301
+ MessagesPlaceholder(variable_name="chat_history"),
302
+ human_message_template
303
+ ])
304
+
305
+ print("ChatPromptTemplate updated successfully for history management.")
306
+
307
+ def gradio_interface_fn(user_query: str, history: list):
308
+ ai_text_response, generated_image_output, audio_output_path, updated_chat_history = handle_user_input(user_query, history)
309
+ print(f"DEBUG: Type of generated_image_output: {type(generated_image_output)}")
310
+ print(f"DEBUG: Is generated_image_output None?: {generated_image_output is None}")
311
+ if generated_image_output is not None:
312
+ print(f"DEBUG: Generated image mode: {generated_image_output.mode}, size: {generated_image_output.size}")
313
+ return ai_text_response, generated_image_output, audio_output_path, updated_chat_history
314
+
315
+ print("Gradio interface function 'gradio_interface_fn' updated to manage history and audio.")
316
+
317
+ get_ipython().system('pip install bcrypt')
318
+ print("bcrypt library installed successfully.")
319
+
320
+ # Commented out IPython magic to ensure Python compatibility.
321
+ # %%writefile app.py
322
+ # import gradio as gr
323
+ # import torch
324
+ # from diffusers import StableDiffusionPipeline
325
+ # from langchain_cohere import ChatCohere
326
+ # from langchain_core.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, SystemMessagePromptTemplate, MessagesPlaceholder
327
+ # from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
328
+ # from gtts import gTTS
329
+ # import os
330
+ # import base64
331
+ # import json
332
+ # import bcrypt # Import bcrypt
333
+ #
334
+ # # --- Configuration and Initialization (from your notebook) ---
335
+ # COHERE_API_KEY = os.environ.get('COHERE_API_KEY')
336
+ #
337
+ # # --- User Management Functions (from previous steps) ---
338
+ # USERS_FILE = 'users.json'
339
+ # CHAT_HISTORY_FILE_PATTERN = 'chat_history_{}.json' # Pattern for user-specific chat history files
340
+ #
341
+ # def load_users():
342
+ # """Loads user data from the USERS_FILE. Returns an empty dictionary if the file doesn't exist."""
343
+ # if os.path.exists(USERS_FILE):
344
+ # with open(USERS_FILE, 'r') as f:
345
+ # return json.load(f)
346
+ # return {}
347
+ #
348
+ # def save_users(users):
349
+ # """Saves user data to the USERS_FILE."""
350
+ # with open(USERS_FILE, 'w') as f:
351
+ # json.dump(users, f, indent=4)
352
+ #
353
+ # def hash_password(password):
354
+ # """
355
+ # Hashes a password using bcrypt.
356
+ # The password is encoded to bytes, a salt is generated, and then the password is hashed.
357
+ # Returns the hashed password as a UTF-8 decoded string.
358
+ # """
359
+ # hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
360
+ # return hashed.decode('utf-8')
361
+ #
362
+ # def register_user(username, password):
363
+ # """
364
+ # Registers a new user with the given username and password.
365
+ # Hashes the password and saves it to users.json.
366
+ # Handles cases where the username already exists.
367
+ # """
368
+ # users = load_users()
369
+ # if username in users:
370
+ # return "Error: Username already exists."
371
+ #
372
+ # hashed_password = hash_password(password)
373
+ # users[username] = hashed_password
374
+ # save_users(users)
375
+ # # Initialize an empty chat history for the new user
376
+ # save_chat_history(username, [])
377
+ # return f"User '{username}' registered successfully."
378
+ #
379
+ # def authenticate_user(username, password):
380
+ # """
381
+ # Authenticates a user against the stored hashed passwords in users.json.
382
+ # Returns True if authentication is successful, False otherwise.
383
+ # """
384
+ # users = load_users()
385
+ # stored_hashed_password = users.get(username)
386
+ #
387
+ # if stored_hashed_password:
388
+ # try:
389
+ # return bcrypt.checkpw(password.encode('utf-8'), stored_hashed_password.encode('utf-8'))
390
+ # except ValueError:
391
+ # return False
392
+ # return False
393
+ #
394
+ # def load_chat_history(username):
395
+ # """
396
+ # Loads a user's chat history from their specific JSON file.
397
+ # Converts stored dicts back into Langchain HumanMessage/AIMessage objects.
398
+ # """
399
+ # history_file = CHAT_HISTORY_FILE_PATTERN.format(username)
400
+ # if os.path.exists(history_file):
401
+ # with open(history_file, 'r') as f:
402
+ # raw_history = json.load(f)
403
+ # chat_history = []
404
+ # for msg in raw_history:
405
+ # if msg['type'] == 'human':
406
+ # chat_history.append(HumanMessage(content=msg['content']))
407
+ # elif msg['type'] == 'ai':
408
+ # chat_history.append(AIMessage(content=msg['content']))
409
+ # elif msg['type'] == 'system':
410
+ # chat_history.append(SystemMessage(content=msg['content']))
411
+ # return chat_history
412
+ # return []
413
+ #
414
+ # def save_chat_history(username, chat_history):
415
+ # """
416
+ # Saves a user's chat history (Langchain Message objects) to their specific JSON file.
417
+ # Converts Langchain Message objects to dictionaries for JSON serialization.
418
+ # """
419
+ # history_file = CHAT_HISTORY_FILE_PATTERN.format(username)
420
+ # raw_history = []
421
+ # for msg in chat_history:
422
+ # raw_history.append({'type': msg.type, 'content': msg.content})
423
+ # with open(history_file, 'w') as f:
424
+ # json.dump(raw_history, f, indent=4)
425
+ #
426
+ # def convert_langchain_to_gradio_chatbot_history(langchain_history):
427
+ # """
428
+ # Converts a list of Langchain Message objects to Gradio Chatbot format.
429
+ # Gradio Chatbot expects a list of lists: [[user_message, ai_message], ...]
430
+ # """
431
+ # gradio_history = []
432
+ # # Ensure we iterate in pairs (human, AI)
433
+ # for i in range(0, len(langchain_history), 2):
434
+ # human_msg = langchain_history[i].content if i < len(langchain_history) else ""
435
+ # # Check if there's a corresponding AI message
436
+ # ai_msg = langchain_history[i+1].content if i+1 < len(langchain_history) else ""
437
+ # gradio_history.append([human_msg, ai_msg])
438
+ # return gradio_history
439
+ #
440
+ # # Initialize users.json if it doesn't exist
441
+ # if not os.path.exists(USERS_FILE):
442
+ # save_users({}) # Save an empty dictionary as a valid JSON
443
+ #
444
+ #
445
+ # # --- Stable Diffusion Model Initialization ---
446
+ # model_id = "runwayml/stable-diffusion-v1-5"
447
+ # device = "cuda" if torch.cuda.is_available() else "cpu"
448
+ # pipeline = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
449
+ # pipeline.to(device)
450
+ #
451
+ # # --- Cohere LLM Initialization ---
452
+ # llm = ChatCohere(cohere_api_key=COHERE_API_KEY)
453
+ #
454
+ # # --- Prompt Templates ---
455
+ # system_message_template = SystemMessagePromptTemplate.from_template(
456
+ # "You are a helpful AI assistant that can generate creative images based on user descriptions. "
457
+ # "You can also answer questions and engage in conversation. "
458
+ # "If a user asks for an image, extract the key elements for a Stable Diffusion prompt and output it in the format 'IMAGE_PROMPT: <your image prompt here>'. "
459
+ # "Otherwise, provide a normal conversational response."
460
+ # )
461
+ # human_message_template = HumanMessagePromptTemplate.from_template("User: {user_input}")
462
+ # chat_prompt_template = ChatPromptTemplate.from_messages([
463
+ # system_message_template,
464
+ # MessagesPlaceholder(variable_name="chat_history"),
465
+ # human_message_template
466
+ # ])
467
+ #
468
+ # # --- Core Logic Functions ---
469
+ # def handle_user_input(user_message: str, chat_history: list, username: str):
470
+ # current_human_message = HumanMessage(content=user_message)
471
+ # system_message_content = system_message_template.format().content
472
+ # system_message = SystemMessage(content=system_message_content)
473
+ # messages_for_llm = [system_message] + chat_history + [current_human_message]
474
+ #
475
+ # ai_response = llm.invoke(messages_for_llm)
476
+ # response_content = ai_response.content
477
+ #
478
+ # generated_image = None
479
+ # full_response = ""
480
+ # audio_output_path = None
481
+ #
482
+ # IMAGE_PROMPT_PREFIX = "IMAGE_PROMPT: "
483
+ # if response_content.startswith(IMAGE_PROMPT_PREFIX):
484
+ # image_description = response_content[len(IMAGE_PROMPT_PREFIX):].strip()
485
+ # try:
486
+ # generated_image = pipeline(image_description).images[0]
487
+ # full_response = f"Image generated successfully based on your request: '{image_description}'"
488
+ # except Exception as e:
489
+ # full_response = f"Could not generate image for '{image_description}'. Error: {e}"
490
+ # generated_image = None
491
+ # else:
492
+ # full_response = response_content
493
+ #
494
+ # try:
495
+ # tts = gTTS(text=full_response, lang='es')
496
+ # audio_file_name = f"ai_response_{username}.mp3" if username else "ai_response_guest.mp3"
497
+ # tts.save(audio_file_name)
498
+ # audio_output_path = audio_file_name
499
+ # except Exception as e:
500
+ # audio_output_path = None
501
+ #
502
+ # chat_history.append(current_human_message)
503
+ # chat_history.append(AIMessage(content=ai_response.content))
504
+ #
505
+ # if username:
506
+ # save_chat_history(username, chat_history)
507
+ #
508
+ # return full_response, generated_image, audio_output_path, chat_history
509
+ #
510
+ #
511
+ # def gradio_interface_fn(user_query: str, history: list, username: str):
512
+ # if not user_query: # Handle empty input case
513
+ # return "Por favor, introduce un mensaje.", None, None, history, convert_langchain_to_gradio_chatbot_history(history)
514
+ #
515
+ # ai_text_response, generated_image_output, audio_output_path, updated_chat_history_langchain = handle_user_input(user_query, history, username)
516
+ # gradio_chat_history_display = convert_langchain_to_gradio_chatbot_history(updated_chat_history_langchain)
517
+ # return ai_text_response, generated_image_output, audio_output_path, updated_chat_history_langchain, gradio_chat_history_display
518
+ #
519
+ # # --- Gradio Registration Function ---
520
+ # def register_gradio_fn(username, password):
521
+ # return register_user(username, password)
522
+ #
523
+ # # --- Gradio Login Function ---
524
+ # def login_gradio_fn(username, password):
525
+ # if authenticate_user(username, password):
526
+ # user_history_langchain = load_chat_history(username)
527
+ # gradio_chat_history_display = convert_langchain_to_gradio_chatbot_history(user_history_langchain)
528
+ # return (
529
+ # f"隆Bienvenido, {username}! Has iniciado sesi贸n con 茅xito.",
530
+ # username,
531
+ # user_history_langchain,
532
+ # gradio_chat_history_display
533
+ # )
534
+ # else:
535
+ # return (
536
+ # "Error: Nombre de usuario o contrase帽a incorrectos.",
537
+ # '',
538
+ # [],
539
+ # []
540
+ # )
541
+ #
542
+ # # --- Gradio Logout Function ---
543
+ # def logout_gradio_fn():
544
+ # return (
545
+ # "Has cerrado sesi贸n.",
546
+ # '',
547
+ # [],
548
+ # []
549
+ # )
550
+ #
551
+ # # --- Gradio UI ---
552
+ # image_filename = "Chaty (1).png"
553
+ # base64_image_tag = ""
554
+ # favicon_data_uri = ""
555
+ #
556
+ # if os.path.exists(image_filename):
557
+ # try:
558
+ # with open(image_filename, "rb") as image_file:
559
+ # encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
560
+ # base64_image_tag = f"<img src='data:image/png;base64,{encoded_string}' alt='Chaty Logo' style='height:100px;'>"
561
+ # favicon_data_uri = f"data:image/png;base64,{encoded_string}"
562
+ # except Exception as e:
563
+ # pass # Handle error silently in UI context
564
+ #
565
+ # head_content = ""
566
+ # if favicon_data_uri:
567
+ # head_content = f"<link rel='icon' type='image/png' href='{favicon_data_uri}'>"
568
+ #
569
+ # with gr.Blocks(
570
+ # title='Chaty',
571
+ # theme=gr.themes.Soft(),
572
+ # head=head_content
573
+ # ) as demo:
574
+ # logged_in_user = gr.State('') # Initialize logged_in_user state
575
+ # chatbot_history_state = gr.State([]) # State for Langchain messages
576
+ #
577
+ # gr.Markdown(f"{base64_image_tag} Un asistente de IA interactivo que utiliza Stable Diffusion para la generaci\u00f3n de im\u00e1genes y Cohere/Langchain para la comprensi\u00f3n del lenguaje y la generaci\u00f3n de respuestas. Si pides una imagen, la generar\u00e1; de lo contrario, responder\u00e1 conversacionalmente. **\u00a1Ahora con respuestas de voz!**")
578
+ #
579
+ # # Define chatbot UI elements at a higher scope
580
+ # chatbot_ui_elements_column = gr.Column(visible=True)
581
+ # with chatbot_ui_elements_column:
582
+ # chatbot_display_chatbot = gr.Chatbot(label='Chaty', value=[], elem_id='chatbot', height=400)
583
+ # chatbot_input_textbox = gr.Textbox(lines=2, label='Escribe tu mensaje o petici贸n de imagen aqu铆...', interactive=True)
584
+ # chatbot_submit_button = gr.Button("Enviar", interactive=True)
585
+ # ai_response_textbox = gr.Textbox(lines=5, label='Respuesta de la IA', interactive=False)
586
+ # image_output = gr.Image(label='Imagen Generada')
587
+ # audio_output = gr.Audio(label='Respuesta de Voz', autoplay=True)
588
+ #
589
+ # with gr.Tab("Inicio de Sesi贸n"):
590
+ # gr.Markdown("## Iniciar Sesi贸n")
591
+ # with gr.Column():
592
+ # login_username = gr.Textbox(label="Nombre de Usuario")
593
+ # login_password = gr.Textbox(label="Contrase帽a", type="password")
594
+ # login_button = gr.Button("Iniciar Sesi贸n")
595
+ # login_output = gr.Textbox(label="Mensaje de Inicio de Sesi贸n", interactive=False)
596
+ # current_logged_in_user_display = gr.Textbox(label="Usuario Actual", interactive=False, value="No has iniciado sesi贸n.")
597
+ # logout_button = gr.Button("Cerrar Sesi贸n")
598
+ #
599
+ # login_button.click(
600
+ # fn=login_gradio_fn,
601
+ # inputs=[login_username, login_password],
602
+ # outputs=[
603
+ # login_output,
604
+ # logged_in_user,
605
+ # chatbot_history_state,
606
+ # chatbot_display_chatbot
607
+ # ]
608
+ # )
609
+ #
610
+ # logout_button.click(
611
+ # fn=logout_gradio_fn,
612
+ # inputs=[],
613
+ # outputs=[
614
+ # login_output,
615
+ # logged_in_user,
616
+ # chatbot_history_state,
617
+ # chatbot_display_chatbot
618
+ # ]
619
+ # )
620
+ #
621
+ # logged_in_user.change(
622
+ # lambda user: f"Has iniciado sesi贸n como: {user}" if user else "No has iniciado sesi贸n.",
623
+ # inputs=logged_in_user,
624
+ # outputs=current_logged_in_user_display
625
+ # )
626
+ #
627
+ # with gr.Tab("Chatbot"):
628
+ # chatbot_submit_button.click(
629
+ # fn=gradio_interface_fn,
630
+ # inputs=[chatbot_input_textbox, chatbot_history_state, logged_in_user], # Removed audio_input
631
+ # outputs=[
632
+ # ai_response_textbox,
633
+ # image_output,
634
+ # audio_output,
635
+ # chatbot_history_state,
636
+ # chatbot_display_chatbot
637
+ # ]
638
+ # ).then(
639
+ # lambda: "", # Clear the input textbox after submission
640
+ # inputs=None,
641
+ # outputs=chatbot_input_textbox
642
+ # )
643
+ #
644
+ # with gr.Tab("Registro de Usuario"):
645
+ # gr.Markdown("## Registrar Nuevo Usuario")
646
+ # with gr.Column():
647
+ # register_username = gr.Textbox(label="Nombre de Usuario")
648
+ # register_password = gr.Textbox(label="Contrase帽a", type="password")
649
+ # register_button = gr.Button("Registrar")
650
+ # register_output = gr.Textbox(label="Mensaje de Registro", interactive=False)
651
+ #
652
+ # register_button.click(
653
+ # fn=register_gradio_fn,
654
+ # inputs=[register_username, register_password],
655
+ # outputs=register_output
656
+ # )
657
+ #
658
+ # demo.launch(debug=True, share=True)
659
+
660
+ !pip install langchain-cohere
661
+ get_ipython().system('python app.py')