Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -639,38 +639,43 @@ class XylariaChat:
|
|
| 639 |
|
| 640 |
def streaming_response(message, chat_history, image_filepath, math_ocr_image_path):
|
| 641 |
if message.strip().lower() == "/image":
|
| 642 |
-
|
| 643 |
-
yield "", chat_history, None, None
|
| 644 |
-
|
| 645 |
image_prompt = ""
|
| 646 |
-
|
| 647 |
-
for i in range(len(chat_history) - 2, -1, -1):
|
| 648 |
if chat_history[i][0] and chat_history[i][0].strip().lower() != "/image":
|
| 649 |
image_prompt = chat_history[i][0]
|
| 650 |
break
|
| 651 |
-
|
| 652 |
if not image_prompt:
|
| 653 |
image_prompt = "A realistic image"
|
| 654 |
|
|
|
|
| 655 |
image_bytes = self.generate_image(image_prompt)
|
| 656 |
|
| 657 |
if isinstance(image_bytes, bytes):
|
| 658 |
base64_image = base64.b64encode(image_bytes).decode("utf-8")
|
| 659 |
image_html = f'<img src="data:image/png;base64,{base64_image}" alt="Generated Image" style="max-width: 100%; max-height: 400px;">'
|
| 660 |
|
| 661 |
-
#
|
| 662 |
-
chat_history[
|
| 663 |
-
|
|
|
|
|
|
|
| 664 |
self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
|
| 665 |
-
self.conversation_history.append(ChatMessage(role="assistant", content=image_html).to_dict())
|
| 666 |
|
| 667 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
else:
|
| 669 |
-
chat_history[
|
| 670 |
-
yield "", chat_history, None, None
|
| 671 |
return
|
| 672 |
|
| 673 |
-
|
| 674 |
if math_ocr_image_path:
|
| 675 |
ocr_text = self.perform_math_ocr(math_ocr_image_path)
|
| 676 |
if ocr_text.startswith("Error"):
|
|
|
|
| 639 |
|
| 640 |
def streaming_response(message, chat_history, image_filepath, math_ocr_image_path):
|
| 641 |
if message.strip().lower() == "/image":
|
| 642 |
+
# Extract image generation prompt from the last user message before /image
|
|
|
|
|
|
|
| 643 |
image_prompt = ""
|
| 644 |
+
for i in range(len(chat_history) - 1, -1, -1):
|
|
|
|
| 645 |
if chat_history[i][0] and chat_history[i][0].strip().lower() != "/image":
|
| 646 |
image_prompt = chat_history[i][0]
|
| 647 |
break
|
| 648 |
+
|
| 649 |
if not image_prompt:
|
| 650 |
image_prompt = "A realistic image"
|
| 651 |
|
| 652 |
+
# Generate image based on the extracted prompt
|
| 653 |
image_bytes = self.generate_image(image_prompt)
|
| 654 |
|
| 655 |
if isinstance(image_bytes, bytes):
|
| 656 |
base64_image = base64.b64encode(image_bytes).decode("utf-8")
|
| 657 |
image_html = f'<img src="data:image/png;base64,{base64_image}" alt="Generated Image" style="max-width: 100%; max-height: 400px;">'
|
| 658 |
|
| 659 |
+
# Append the /image command and generated image to chat history
|
| 660 |
+
chat_history.append([message, ""])
|
| 661 |
+
chat_history.append(["", image_html])
|
| 662 |
+
|
| 663 |
+
# Update conversation history
|
| 664 |
self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
|
| 665 |
+
self.conversation_history.append(ChatMessage(role="assistant", content=image_html).to_dict())
|
| 666 |
|
| 667 |
+
# Update the chat history file
|
| 668 |
+
self.save_chat()
|
| 669 |
+
all_chats = self.load_all_chats()
|
| 670 |
+
chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
|
| 671 |
+
|
| 672 |
+
yield "", chat_history, None, None, gr.update(choices=chat_titles, visible=True)
|
| 673 |
else:
|
| 674 |
+
chat_history.append([message, image_bytes])
|
| 675 |
+
yield "", chat_history, None, None, None
|
| 676 |
return
|
| 677 |
|
| 678 |
+
ocr_text = ""
|
| 679 |
if math_ocr_image_path:
|
| 680 |
ocr_text = self.perform_math_ocr(math_ocr_image_path)
|
| 681 |
if ocr_text.startswith("Error"):
|