0Learn commited on
Commit
922583c
·
verified ·
1 Parent(s): 17a6709

updated the UI to have title

Browse files
Files changed (1) hide show
  1. app.py +34 -14
app.py CHANGED
@@ -7,6 +7,7 @@ BACKEND_URL = "https://0learn-fastchat.hf.space" # Update this with your actual
7
  class ConversationState:
8
  def __init__(self):
9
  self.conversation_id = None
 
10
 
11
  state = ConversationState()
12
 
@@ -22,39 +23,58 @@ def chat_with_groq(message, history):
22
  response_data = response.json()
23
 
24
  state.conversation_id = response_data["conversation_id"]
 
 
 
 
 
25
  return response_data["response"]
26
 
27
  def load_conversation(conversation_id):
28
  if not conversation_id:
29
- return []
30
  response = requests.get(f"{BACKEND_URL}/conversations/{conversation_id}")
31
  if response.status_code == 200:
32
  conversation_data = response.json()
33
  state.conversation_id = conversation_id
34
  messages = conversation_data["messages"]
35
- return [(msg["content"], messages[i+1]["content"]) for i, msg in enumerate(messages[::2])]
 
 
 
 
 
 
 
 
 
 
 
 
36
  else:
37
- return []
38
 
39
  with gr.Blocks() as demo:
40
- gr.Markdown("# Groq Chatbot with Conversation Persistence")
41
 
42
- conversation_id_input = gr.Textbox(label="Conversation ID (optional)")
43
- load_button = gr.Button("Load Conversation")
 
 
 
44
 
45
  chatbot = gr.Chatbot()
46
- msg = gr.Textbox()
47
- clear = gr.Button("Clear")
48
 
49
  def respond(message, chat_history):
50
  bot_message = chat_with_groq(message, chat_history)
51
  chat_history.append((message, bot_message))
52
- return "", chat_history
53
-
54
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
55
- clear.click(lambda: None, None, chatbot, queue=False)
56
- load_button.click(load_conversation, inputs=[conversation_id_input], outputs=[chatbot])
57
 
58
- gr.Markdown(f"## Current Conversation ID: {state.conversation_id}")
 
 
 
59
 
60
  demo.launch()
 
7
  class ConversationState:
8
  def __init__(self):
9
  self.conversation_id = None
10
+ self.title = "New Conversation"
11
 
12
  state = ConversationState()
13
 
 
23
  response_data = response.json()
24
 
25
  state.conversation_id = response_data["conversation_id"]
26
+
27
+ # Update title if it's a new conversation
28
+ if len(history) == 0:
29
+ state.title = message[:30] + "..." if len(message) > 30 else message
30
+
31
  return response_data["response"]
32
 
33
  def load_conversation(conversation_id):
34
  if not conversation_id:
35
+ return [], state.title
36
  response = requests.get(f"{BACKEND_URL}/conversations/{conversation_id}")
37
  if response.status_code == 200:
38
  conversation_data = response.json()
39
  state.conversation_id = conversation_id
40
  messages = conversation_data["messages"]
41
+ state.title = messages[0]["content"][:30] + "..." if len(messages[0]["content"]) > 30 else messages[0]["content"]
42
+ return [(msg["content"], messages[i+1]["content"]) for i, msg in enumerate(messages[::2])], state.title
43
+ else:
44
+ return [], "Conversation not found"
45
+
46
+ def start_new_conversation():
47
+ state.conversation_id = None
48
+ state.title = "New Conversation"
49
+ return [], state.title
50
+
51
+ def continue_conversation():
52
+ if state.conversation_id:
53
+ return load_conversation(state.conversation_id)
54
  else:
55
+ return [], "No previous conversation"
56
 
57
  with gr.Blocks() as demo:
58
+ gr.Markdown("# Groq Chatbot with Enhanced Conversation Management")
59
 
60
+ with gr.Row():
61
+ new_conv_btn = gr.Button("New Conversation")
62
+ continue_conv_btn = gr.Button("Continue Previous Conversation")
63
+
64
+ title_display = gr.Markdown(f"## Current Conversation: {state.title}")
65
 
66
  chatbot = gr.Chatbot()
67
+ msg = gr.Textbox(label="Type your message here")
68
+ clear = gr.Button("Clear Chat")
69
 
70
  def respond(message, chat_history):
71
  bot_message = chat_with_groq(message, chat_history)
72
  chat_history.append((message, bot_message))
73
+ return "", chat_history, state.title
 
 
 
 
74
 
75
+ msg.submit(respond, [msg, chatbot], [msg, chatbot, title_display])
76
+ clear.click(lambda: ([], state.title), None, [chatbot, title_display], queue=False)
77
+ new_conv_btn.click(start_new_conversation, None, [chatbot, title_display])
78
+ continue_conv_btn.click(continue_conversation, None, [chatbot, title_display])
79
 
80
  demo.launch()