elapt1c commited on
Commit
37a3f4f
·
verified ·
1 Parent(s): ecd8dab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -4
app.py CHANGED
@@ -79,13 +79,33 @@ def chat(message: str, conversation_history: ConversationHistory) -> Generator[T
79
  # Yield formatted history and updated conversation history
80
  yield conversation_history.get_formatted_history(), conversation_history
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  chatbot = gr.Chatbot(label="Chat")
83
  conversation_history = ConversationHistory() # Initialize conversation history
84
 
85
  iface = gr.Interface(
86
- fn=chat,
87
- inputs=[gr.Textbox(placeholder="Hello! How are you?", label="Message")],
88
- outputs=[chatbot, conversation_history],
89
- allow_flagging="never",
90
  )
 
91
  iface.queue().launch()
 
79
  # Yield formatted history and updated conversation history
80
  yield conversation_history.get_formatted_history(), conversation_history
81
 
82
+ # Create a custom Gradio component to display the conversation history
83
+ class ConversationHistoryComponent(gr.Component):
84
+ def __init__(self, **kwargs):
85
+ super().__init__(**kwargs)
86
+ self.history = []
87
+
88
+ def build(self):
89
+ return gr.HTML(value="<div id='conversation-history'></div>")
90
+
91
+ def update(self, history: List[Tuple[str, str]]):
92
+ history_html = "<ul>"
93
+ for user_message, assistant_message in history:
94
+ history_html += f"<li><strong>User:</strong> {user_message}</li>"
95
+ if assistant_message is not None:
96
+ history_html += f"<li><strong>Assistant:</strong> {assistant_message}</li>"
97
+ history_html += "</ul>"
98
+ self.history = history
99
+ return gr.HTML(value=history_html)
100
+
101
  chatbot = gr.Chatbot(label="Chat")
102
  conversation_history = ConversationHistory() # Initialize conversation history
103
 
104
  iface = gr.Interface(
105
+ fn=chat,
106
+ inputs=[gr.Textbox(placeholder="Hello! How are you?", label="Message")],
107
+ outputs=[chatbot, ConversationHistoryComponent(label="Conversation History")],
108
+ allow_flagging="never",
109
  )
110
+
111
  iface.queue().launch()