Spaces:
Running
Running
Update app.py
Browse files625 unmodified lines6266276286296306312 unmodified lines6346356366376386396405 unmodified lines6466476486496506516526536546556566576586596606616626631 unmodified line6656666676686696706715 unmodified lines6776786796806816826831 unmodified line6856866876886896906913 unmodified lines695696697698699700701625 unmodified linesdef create_interface(self):def streaming_response(message, chat_history, image_filepath, math_ocr_image_path):if message.strip().lower().startswith("/image"):image_prompt = message.strip().lower()[len("/image"):].strip()2 unmodified lineschat_history.append([message, ""])yield "", chat_history, None, Noneimage_bytes = self.generate_image(image_prompt)5 unmodified lineschat_history[-1] = ("", image_html)self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())self.conversation_history.append(ChatMessage(role="assistant", content=image_html).to_dict())self.save_chat()all_chats = self.load_all_chats()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]yield "", chat_history, None, Noneelse:chat_history[-1] = ("", image_bytes)yield "", chat_history, None, Nonereturnocr_text = ""1 unmodified lineocr_text = self.perform_math_ocr(math_ocr_image_path)if ocr_text.startswith("Error"):updated_history = chat_history + [[message, ocr_text]]yield "", updated_history, None, Nonereturnelse:message = f"Math OCR Result: {ocr_text}\n\nUser's message: {message}"5 unmodified linesif isinstance(response_stream, str):updated_history = chat_history + [[message, response_stream]]yield "", updated_history, None, Nonereturnfull_response = ""1 unmodified lineif isinstance(response_stream, str):updated_history = chat_history + [[message, response_stream]]yield "", updated_history, None, Nonereturntry:3 unmodified linesfull_response += chunk.token.textupdated_history[-1][1] = full_responseyield "", updated_history, None, Noneexcept Exception as e:print(f"Streaming error: {e}")625 unmodified lines6266276286296306316326336346356366376386396406416426436446456466476486496506516526536546552 unmodified lines6586596606616626636646655 unmodified lines6716726736746756766776786796806816826836846856866876886896901 unmodified line6926936946956966976985 unmodified lines7047057067077087097101 unmodified line7127137147157167177183 unmodified lines722723724725726727728729730731732733625 unmodified linesdef create_interface(self):def streaming_response(message, chat_history, image_filepath, math_ocr_image_path):# Placeholder for image generationloading_svg = """<svg width="256" height="256" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg"><style>rect {animation: fillAnimation 3s ease-in-out infinite;}@keyframes fillAnimation {0% { fill: #626262; }50% { fill: #111111; }100% { fill: #626262; }}text {font-family: 'Helvetica Neue', Arial, sans-serif;font-weight: 300;text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.4);}</style><rect width="256" height="256" rx="20" fill="#888888" /><text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-size="24" fill="white" opacity="0.8"><tspan>{/}</tspan><tspan x="50%" dy="1.2em"></tspan></text></svg>"""if message.strip().lower().startswith("/image"):image_prompt = message.strip().lower()[len("/image"):].strip()2 unmodified lineschat_history.append([message, ""])chat_history.append(("", loading_svg))yield "", chat_history, None, None, Noneimage_bytes = self.generate_image(image_prompt)5 unmodified lineschat_history[-1] = ("", image_html)self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())self.conversation_history.append(ChatMessage(role="assistant", content=image_html).to_dict())self.save_chat()all_chats = self.load_all_chats()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]yield "", chat_history, None, None, gr.update(choices=chat_titles, visible=True)else:chat_history[-1] = ("", image_bytes)yield "", chat_history, None, None, Nonereturnocr_text = ""1 unmodified lineocr_text = self.perform_math_ocr(math_ocr_image_path)if ocr_text.startswith("Error"):updated_history = chat_history + [[message, ocr_text]]yield "", updated_history, None, None, Nonereturnelse:message = f"Math OCR Result: {ocr_text}\n\nUser's message: {message}"5 unmodified linesif isinstance(response_stream, str):updated_history = chat_history + [[message, response_stream]]yield "", updated_history, None, None, Nonereturnfull_response = ""1 unmodified lineif isinstance(response_stream, str):updated_history = chat_history + [[message, response_stream]]yield "", updated_history, None, None, Nonereturntry:3 unmodified linesfull_response += chunk.token.textupdated_history[-1][1] = full_response#self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())#self.conversation_history.append(ChatMessage(role="assistant", content=full_response).to_dict())yield "", updated_history, None, None, Noneexcept Exception as e:print(f"Streaming error: {e}")
app.py+39-7