arjunguha commited on
Commit
2a77a2d
1 Parent(s): 36d1885

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -12
app.py CHANGED
@@ -14,7 +14,7 @@ def generate_conversation_id():
14
  # Handle new chat creation
15
  def new_chat():
16
  conversation_id = generate_conversation_id()
17
- return f"New conversation created. Share this URL to join: /?chat={conversation_id}"
18
 
19
  # Handle user joining the conversation
20
  def join_chat(chat_id, username):
@@ -41,26 +41,43 @@ def chat_interface(chat_id, username, message):
41
  # Gradio app
42
  def main():
43
  with gr.Blocks() as demo:
44
- gr.Markdown("# Chat Application\nClick 'New Chat' to start a new conversation or join an existing one.")
45
- new_chat_button = gr.Button("New Chat")
46
- new_chat_output = gr.Textbox(label="New Chat URL")
47
 
48
- new_chat_button.click(fn=new_chat, outputs=new_chat_output)
 
 
 
49
 
50
- with gr.Row():
 
 
51
  chat_id = gr.Textbox(label="Chat ID", placeholder="Enter Chat ID to join or create a new one.")
52
  username = gr.Textbox(label="Username", placeholder="Enter your username.")
53
  join_button = gr.Button("Join Chat")
54
  join_output = gr.Textbox(label="Join Status")
55
-
56
- join_button.click(fn=join_chat, inputs=[chat_id, username], outputs=join_output)
 
57
 
58
- message = gr.Textbox(label="Your Message", placeholder="Type your message here.")
59
- send_button = gr.Button("Send Message")
60
- chat_output = gr.Textbox(label="Chat Messages")
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  send_button.click(fn=chat_interface, inputs=[chat_id, username, message], outputs=chat_output)
63
-
64
  demo.launch()
65
 
66
  if __name__ == "__main__":
 
14
  # Handle new chat creation
15
  def new_chat():
16
  conversation_id = generate_conversation_id()
17
+ return conversation_id
18
 
19
  # Handle user joining the conversation
20
  def join_chat(chat_id, username):
 
41
  # Gradio app
42
  def main():
43
  with gr.Blocks() as demo:
44
+ gr.Markdown("# Chat Application\nChoose an option to start or join a conversation.")
 
 
45
 
46
+ # Initial buttons for New Chat or Enter Conversation ID
47
+ with gr.Column() as initial_buttons:
48
+ new_chat_button = gr.Button("New Chat")
49
+ existing_chat_button = gr.Button("Enter Conversation ID for Existing Chat")
50
 
51
+ # Chat interface components, initially hidden
52
+ with gr.Column(visible=False) as chat_interface:
53
+ chat_id_display = gr.Markdown("Chat ID: ")
54
  chat_id = gr.Textbox(label="Chat ID", placeholder="Enter Chat ID to join or create a new one.")
55
  username = gr.Textbox(label="Username", placeholder="Enter your username.")
56
  join_button = gr.Button("Join Chat")
57
  join_output = gr.Textbox(label="Join Status")
58
+ message = gr.Textbox(label="Your Message", placeholder="Type your message here.")
59
+ send_button = gr.Button("Send Message")
60
+ chat_output = gr.Textbox(label="Chat Messages")
61
 
62
+ # New Chat button logic
63
+ def handle_new_chat():
64
+ conversation_id = new_chat()
65
+ return gr.update(visible=True), gr.update(value=f"Chat ID: {conversation_id}"), conversation_id
66
 
67
+ new_chat_button.click(fn=handle_new_chat, outputs=[chat_interface, chat_id_display, chat_id])
68
+
69
+ # Existing Chat button logic
70
+ def handle_existing_chat():
71
+ return gr.update(visible=True)
72
+
73
+ existing_chat_button.click(fn=handle_existing_chat, outputs=chat_interface)
74
+
75
+ # Join Chat button logic
76
+ join_button.click(fn=join_chat, inputs=[chat_id, username], outputs=join_output)
77
+
78
+ # Send Message button logic
79
  send_button.click(fn=chat_interface, inputs=[chat_id, username, message], outputs=chat_output)
80
+
81
  demo.launch()
82
 
83
  if __name__ == "__main__":