Yoxas commited on
Commit
2224afc
·
verified ·
1 Parent(s): 0a62efe

Update src/interface.py

Browse files
Files changed (1) hide show
  1. src/interface.py +28 -34
src/interface.py CHANGED
@@ -1,22 +1,20 @@
1
  import csv
2
  import gradio as gr
3
 
4
- # Define a function to export conversations to CSV
5
- def export_conversations(conversations):
6
- # Create a CSV writer
7
- csvfile = "conversations.csv"
8
- with open(csvfile, "w", newline="") as f:
9
- writer = csv.writer(f)
10
-
11
- # Write the header row
12
- writer.writerow(["User Input", "Model Response"])
13
-
14
- # Iterate over the conversations and write each row
15
- for conversation in conversations:
16
- for i, (user_input, model_response) in enumerate(conversation):
17
- writer.writerow([user_input, model_response])
18
-
19
- return csvfile
20
 
21
  # Gradio application setup
22
  def create_demo():
@@ -73,26 +71,22 @@ def create_demo():
73
  submit_button = gr.Button('Send')
74
  with gr.Column(scale=0.20):
75
  uploaded_pdf = gr.UploadButton("📁 Upload PDF", file_types=[".pdf"], elem_id='upload_pdf')
 
 
 
 
 
 
 
 
 
 
 
76
 
77
- # Define the Gradio interface
78
- iface = Interface(
79
- fn=export_conversations,
80
- inputs="conversations",
81
- outputs=Output.File(type="csv"),
82
- title="Export Conversations",
83
- description="Export conversations as a CSV file"
84
- )
85
-
86
- # Add a download button to trigger the export
87
- download_button = Button("Download Conversations as CSV")
88
-
89
- # Add the components to the interface
90
- iface.launch(inputs=["conversations"], outputs="file", api_name="export_conversations", api_open_browser=False, flagging_callback=None, analytics_enabled=False, enable_queue=True, show_error=True, allow_screenshot=True, allow_flagging=True)
91
-
92
-
93
- return demo, chat_history, show_img, text_input, submit_button, uploaded_pdf, slider_chunk_size,slider_overlap_percentage,slider_temp,slider_k
94
 
95
  if __name__ == '__main__':
96
- demo, chatbot, show_img, text_input, submit_button, uploaded_pdf, slider_chunk_size,slider_overlap_percentage,slider_temp,slider_k = create_demo()
97
  demo.queue()
98
  demo.launch()
 
1
  import csv
2
  import gradio as gr
3
 
4
+ #NEW
5
+ # Function to update chat history and conversation history
6
+ def update_chat_history(user_input, bot_response):
7
+ chat_history.update(value=chat_history.value + [(user_input, bot_response)])
8
+ conversation_history.append([user_input, bot_response])
9
+
10
+ # Function to download conversation history
11
+ def download_conversation_history():
12
+ with open('conversation_history.csv', 'w', newline='') as file:
13
+ writer = csv.writer(file)
14
+ writer.writerow(["User Input", "Bot Response"])
15
+ writer.writerows(conversation_history)
16
+ return "conversation_history.csv"
17
+ #NEW
 
 
18
 
19
  # Gradio application setup
20
  def create_demo():
 
71
  submit_button = gr.Button('Send')
72
  with gr.Column(scale=0.20):
73
  uploaded_pdf = gr.UploadButton("📁 Upload PDF", file_types=[".pdf"], elem_id='upload_pdf')
74
+ #NEW
75
+ with gr.Colum(scale=0.20):
76
+ download_button = gr.Button('Download Conversation History')
77
+
78
+ # Conversation History
79
+ conversation_history = []
80
+
81
+ # Event listeners
82
+ submit_button.click(fn=update_chat_history, inputs=[text_input, chat_history], outputs=chat_history)
83
+ download_button.click(fn=download_conversation_history, outputs="download")
84
+ #NEW
85
 
86
+
87
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  if __name__ == '__main__':
90
+ demo, chatbot, show_img, text_input, submit_button, uploaded_pdf, slider_chunk_size,slider_overlap_percentage,slider_temp,slider_k, download_button = create_demo()
91
  demo.queue()
92
  demo.launch()