prithivMLmods commited on
Commit
4c7e567
β€’
1 Parent(s): edb2d41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -32
app.py CHANGED
@@ -58,14 +58,16 @@ def respond(
58
 
59
  # Function to save chat history to a text file
60
  def save_as_txt(history):
61
- with open("chat_history.txt", "w") as f:
 
62
  for user_message, assistant_message in history:
63
  f.write(f"User: {user_message}\n")
64
  f.write(f"Assistant: {assistant_message}\n")
65
- return "chat_history.txt"
66
 
67
  # Function to save chat history to a DOCX file
68
  def save_as_docx(history):
 
69
  doc = Document()
70
  doc.add_heading('Chat History', 0)
71
 
@@ -73,11 +75,12 @@ def save_as_docx(history):
73
  doc.add_paragraph(f"User: {user_message}")
74
  doc.add_paragraph(f"Assistant: {assistant_message}")
75
 
76
- doc.save("chat_history.docx")
77
- return "chat_history.docx"
78
 
79
  # Function to save chat history to a PDF file
80
  def save_as_pdf(history):
 
81
  buffer = BytesIO()
82
  c = canvas.Canvas(buffer, pagesize=letter)
83
  width, height = letter
@@ -99,12 +102,12 @@ def save_as_pdf(history):
99
  c.save()
100
  buffer.seek(0)
101
 
102
- with open("chat_history.pdf", "wb") as f:
103
  f.write(buffer.read())
104
 
105
- return "chat_history.pdf"
106
 
107
- # Gradio interface
108
  def handle_file_save(history, file_format):
109
  if file_format == "txt":
110
  return save_as_txt(history)
@@ -112,37 +115,15 @@ def handle_file_save(history, file_format):
112
  return save_as_docx(history)
113
  elif file_format == "pdf":
114
  return save_as_pdf(history)
 
115
 
116
- demo = gr.ChatInterface(
117
- respond,
118
- additional_inputs=[
119
- gr.Textbox(value="", label="System message"),
120
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
121
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
122
- gr.Slider(
123
- minimum=0.1,
124
- maximum=1.0,
125
- value=0.95,
126
- step=0.05,
127
- label="Top-P",
128
- ),
129
- gr.Dropdown(
130
- choices=["txt", "docx", "pdf"],
131
- label="Save as",
132
- ),
133
- ],
134
- outputs=[
135
- gr.File(label="Download Chat History"),
136
- ],
137
- css=css,
138
- theme="allenai/gradio-theme",
139
- )
140
-
141
  def save_handler(message, history, system_message, max_tokens, temperature, top_p, file_format):
142
  response = respond(message, history, system_message, max_tokens, temperature, top_p)
143
  saved_file = handle_file_save(history, file_format)
144
  return saved_file
145
 
 
146
  demo = gr.Interface(
147
  fn=save_handler,
148
  inputs=[
 
58
 
59
  # Function to save chat history to a text file
60
  def save_as_txt(history):
61
+ file_path = "chat_history.txt"
62
+ with open(file_path, "w") as f:
63
  for user_message, assistant_message in history:
64
  f.write(f"User: {user_message}\n")
65
  f.write(f"Assistant: {assistant_message}\n")
66
+ return file_path
67
 
68
  # Function to save chat history to a DOCX file
69
  def save_as_docx(history):
70
+ file_path = "chat_history.docx"
71
  doc = Document()
72
  doc.add_heading('Chat History', 0)
73
 
 
75
  doc.add_paragraph(f"User: {user_message}")
76
  doc.add_paragraph(f"Assistant: {assistant_message}")
77
 
78
+ doc.save(file_path)
79
+ return file_path
80
 
81
  # Function to save chat history to a PDF file
82
  def save_as_pdf(history):
83
+ file_path = "chat_history.pdf"
84
  buffer = BytesIO()
85
  c = canvas.Canvas(buffer, pagesize=letter)
86
  width, height = letter
 
102
  c.save()
103
  buffer.seek(0)
104
 
105
+ with open(file_path, "wb") as f:
106
  f.write(buffer.read())
107
 
108
+ return file_path
109
 
110
+ # Function to handle file saving based on format
111
  def handle_file_save(history, file_format):
112
  if file_format == "txt":
113
  return save_as_txt(history)
 
115
  return save_as_docx(history)
116
  elif file_format == "pdf":
117
  return save_as_pdf(history)
118
+ return None
119
 
120
+ # Handler function for Gradio app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  def save_handler(message, history, system_message, max_tokens, temperature, top_p, file_format):
122
  response = respond(message, history, system_message, max_tokens, temperature, top_p)
123
  saved_file = handle_file_save(history, file_format)
124
  return saved_file
125
 
126
+ # Gradio interface
127
  demo = gr.Interface(
128
  fn=save_handler,
129
  inputs=[