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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
app.py CHANGED
@@ -3,6 +3,7 @@ from openai import OpenAI
3
  import os
4
  from fpdf import FPDF # For PDF conversion
5
  from docx import Document # For DOCX conversion
 
6
 
7
  css = '''
8
  .gradio-container{max-width: 1000px !important}
@@ -53,26 +54,25 @@ def respond(
53
  yield response
54
 
55
  def save_as_file(input_text, output_text, conversion_type):
 
56
  if conversion_type == "PDF":
57
  pdf = FPDF()
58
  pdf.add_page()
59
  pdf.set_font("Arial", size=12)
60
  pdf.multi_cell(0, 10, f"User Query: {input_text}\n\nResponse: {output_text}")
61
- file_name = "output.pdf"
62
  pdf.output(file_name)
63
  elif conversion_type == "DOCX":
64
  doc = Document()
65
  doc.add_heading('Conversation', 0)
66
  doc.add_paragraph(f"User Query: {input_text}\n\nResponse: {output_text}")
67
- file_name = "output.docx"
68
  doc.save(file_name)
69
  elif conversion_type == "TXT":
70
- file_name = "output.txt"
71
  with open(file_name, "w") as f:
72
  f.write(f"User Query: {input_text}\n\nResponse: {output_text}")
73
- else:
74
- return None
75
-
76
  return file_name
77
 
78
  def convert_and_download(history, conversion_type):
@@ -85,10 +85,6 @@ def convert_and_download(history, conversion_type):
85
  file_path = save_as_file(input_text, output_text, conversion_type)
86
  return file_path
87
 
88
- def handle_conversion(history, conversion_type):
89
- file_path = convert_and_download(history, conversion_type)
90
- return gr.File(file_path)
91
-
92
  demo = gr.Blocks(css=css)
93
 
94
  with demo:
@@ -98,22 +94,20 @@ with demo:
98
  temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
99
  top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P")
100
 
101
- history = gr.State(value=[])
102
-
103
  chatbot = gr.ChatInterface(
104
  fn=respond,
105
  additional_inputs=[system_message, max_tokens, temperature, top_p],
106
  )
107
-
108
  with gr.Row():
109
  conversion_type = gr.Dropdown(choices=["PDF", "DOCX", "TXT"], label="Conversion Type")
110
  download_button = gr.Button("Convert and Download")
111
 
112
  file_output = gr.File()
113
-
114
  download_button.click(
115
- handle_conversion,
116
- inputs=[history, conversion_type],
117
  outputs=file_output
118
  )
119
 
 
3
  import os
4
  from fpdf import FPDF # For PDF conversion
5
  from docx import Document # For DOCX conversion
6
+ import tempfile
7
 
8
  css = '''
9
  .gradio-container{max-width: 1000px !important}
 
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()
61
  pdf.set_font("Arial", size=12)
62
  pdf.multi_cell(0, 10, f"User Query: {input_text}\n\nResponse: {output_text}")
63
+ file_name = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
64
  pdf.output(file_name)
65
  elif conversion_type == "DOCX":
66
  doc = Document()
67
  doc.add_heading('Conversation', 0)
68
  doc.add_paragraph(f"User Query: {input_text}\n\nResponse: {output_text}")
69
+ file_name = tempfile.NamedTemporaryFile(delete=False, suffix=".docx").name
70
  doc.save(file_name)
71
  elif conversion_type == "TXT":
72
+ file_name = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
73
  with open(file_name, "w") as f:
74
  f.write(f"User Query: {input_text}\n\nResponse: {output_text}")
75
+
 
 
76
  return file_name
77
 
78
  def convert_and_download(history, conversion_type):
 
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:
 
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