prithivMLmods commited on
Commit
fec8e6e
β€’
1 Parent(s): d9bd1ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -10
app.py CHANGED
@@ -22,14 +22,17 @@ client = OpenAI(
22
 
23
  def respond(
24
  message,
25
- history: list[tuple[str, str]],
26
  system_message,
27
  max_tokens,
28
  temperature,
29
  top_p,
30
  ):
31
- messages = [{"role": "system", "content": system_message}]
 
32
 
 
 
33
  for val in history:
34
  if val[0]:
35
  messages.append({"role": "user", "content": val[0]})
@@ -52,9 +55,15 @@ def respond(
52
 
53
  response += token
54
  yield response
 
 
 
55
 
56
- def save_as_file(input_text, output_text, conversion_type):
 
 
57
  file_name = None
 
58
  if conversion_type == "PDF":
59
  pdf = FPDF()
60
  pdf.add_page()
@@ -78,36 +87,36 @@ def save_as_file(input_text, output_text, conversion_type):
78
  def convert_and_download(history, conversion_type):
79
  if not history:
80
  return None
81
-
82
- input_text = "\n".join([f"User: {h[0]}" for h in history if h[0]])
83
- output_text = "\n".join([f"Assistant: {h[1]}" for h in history if h[1]])
84
 
85
- file_path = save_as_file(input_text, output_text, conversion_type)
86
  return file_path
87
 
88
  demo = gr.Blocks(css=css)
89
 
90
  with demo:
 
 
91
  with gr.Row():
92
  system_message = gr.Textbox(value="", label="System message")
93
  max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
94
  temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
95
- top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
96
 
97
  chatbot = gr.ChatInterface(
98
  fn=respond,
99
  additional_inputs=[system_message, max_tokens, temperature, top_p],
 
100
  )
101
 
102
  with gr.Row():
103
- conversion_type = gr.Dropdown(choices=["PDF", "DOCX", "TXT"], label="Conversion Type")
104
  download_button = gr.Button("Convert and Download")
105
 
106
  file_output = gr.File()
107
 
108
  download_button.click(
109
  convert_and_download,
110
- inputs=[chatbot.history, conversion_type],
111
  outputs=file_output
112
  )
113
 
 
22
 
23
  def respond(
24
  message,
25
+ history,
26
  system_message,
27
  max_tokens,
28
  temperature,
29
  top_p,
30
  ):
31
+ if history is None:
32
+ history = []
33
 
34
+ messages = [{"role": "system", "content": system_message}]
35
+
36
  for val in history:
37
  if val[0]:
38
  messages.append({"role": "user", "content": val[0]})
 
55
 
56
  response += token
57
  yield response
58
+
59
+ history.append((message, response))
60
+ return history
61
 
62
+ def save_as_file(history, conversion_type):
63
+ input_text = "\n".join([f"User: {h[0]}" for h in history if h[0]])
64
+ output_text = "\n".join([f"Assistant: {h[1]}" for h in history if h[1]])
65
  file_name = None
66
+
67
  if conversion_type == "PDF":
68
  pdf = FPDF()
69
  pdf.add_page()
 
87
  def convert_and_download(history, conversion_type):
88
  if not history:
89
  return None
 
 
 
90
 
91
+ file_path = save_as_file(history, conversion_type)
92
  return file_path
93
 
94
  demo = gr.Blocks(css=css)
95
 
96
  with demo:
97
+ history_state = gr.State([]) # Initialize an empty list to store the conversation history
98
+
99
  with gr.Row():
100
  system_message = gr.Textbox(value="", label="System message")
101
  max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
102
  temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
103
+ top_p = gr.Slider(minimum=0.1, maximum 1.0, value=0.95, step=0.05, label="Top-P")
104
 
105
  chatbot = gr.ChatInterface(
106
  fn=respond,
107
  additional_inputs=[system_message, max_tokens, temperature, top_p],
108
+ state=history_state, # Pass the history state to the ChatInterface
109
  )
110
 
111
  with gr.Row():
112
+ conversion_type = gr.Dropdown(choices=["PDF", "DOCX", "TXT"], value="PDF", label="Conversion Type")
113
  download_button = gr.Button("Convert and Download")
114
 
115
  file_output = gr.File()
116
 
117
  download_button.click(
118
  convert_and_download,
119
+ inputs=[history_state, conversion_type], # Pass the history state to the conversion function
120
  outputs=file_output
121
  )
122