Open jackllll1312 opened 6 months ago
import toga from toga.style import Pack from toga.style.pack import COLUMN, ROW
class ChatApp(toga.App):
def startup(self):
# Set up main window
self.main_window = toga.MainWindow(title=self.formal_name, size=(600, 400))
# Create a box to hold the conversation and the user input
main_box = toga.Box(style=Pack(direction=COLUMN))
# Create the conversation box with a scrollbar
self.conversation_box = toga.ScrollContainer(style=Pack(direction=COLUMN, flex=1))
# Create the user input box with a send button
self.user_input = toga.TextInput(style=Pack(flex=1))
self.send_button = toga.Button('Send', on_press=self.send_message)
input_box = toga.Box(children=[self.user_input, self.send_button], style=Pack(direction=ROW))
# Add the conversation and the input box to the main box
main_box.add(self.conversation_box)
main_box.add(input_box)
# Set the main window content
self.main_window.content = main_box
self.main_window.show()
def send_message(self, widget):
# Get the user input
user_input = self.user_input.value
# Create chat bubbles for the user and the assistant
if user_input:
# User chat bubble on the right
user_label = toga.Label(user_input, style=Pack(padding_left=10, padding_right=10, padding_top=5, padding_bottom=5, background_color='royalblue', color='white'))
user_chat_bubble = toga.Box(style=Pack(direction=ROW, padding=5, alignment='right'))
user_chat_bubble.add(user_label)
# Assistant chat bubble on the left
assistant_label = toga.Label('...', style=Pack(padding_left=10, padding_right=10, padding_top=5, padding_bottom=5, background_color='lightgray', color='black'))
assistant_chat_bubble = toga.Box(style=Pack(direction=ROW, padding=5, alignment='left'))
assistant_chat_bubble.add(assistant_label)
# Add chat bubbles to the conversation
self.conversation_box.content.add(user_chat_bubble)
self.conversation_box.content.add(assistant_chat_bubble)
# Scroll to the bottom of the conversation box
self.conversation_box.vertical_scroll = self.conversation_box.vertical_scroll.maximum
# Clear the user input
self.user_input.value = ''
# Placeholder for where you would get the actual response
assistant_label.text = 'Assistant response to: ' + user_input
def main(): return ChatApp('Chat App', 'org.beeware.chatapp')
if name == 'main': app = main() app.main_loop()
def create_chat_bubble(self, sender, content):
chat_bubble = toga.Box(style=Pack(padding=10, background_color='#007AFF' if sender == 'User' else '#F0F0F0', border_radius=12))
# Add a label with sender and content
label = toga.Label(f'{content}', style=Pack(color='white' if sender == 'User' else 'black', padding_left=10))
chat_bubble.add(label)
return chat_bubble
def startup(self): ... main_box = toga.Box( children=[conversation_box, input_box], style=Pack(direction=COLUMN, background_color='#131313') # Dark background ) ...
import toga from toga.style import Pack from toga.style.pack import COLUMN, ROW, RIGHT, LEFT from datetime import datetime from openai import AzureOpenAI
class ChatApp(toga.App): def init(self, *args, *kwargs): super().init(args, **kwargs) self.client = AzureOpenAI( api_key=("77a0ca4ac2524d5f97a937bf4b69e305"), api_version="2023-12-01-preview", azure_endpoint=("https://azure-openai-ais-poc.openai.azure.com/") ) self.conversation = [] self.system_message = {"role": "system", "content": "你是個非常有幫助的PWC AI助理,講中文"}
def main(): return ChatApp('Chat App', 'org.beeware.chatapp')
if name == 'main': app = main() app.main_loop()