prithivMLmods commited on
Commit
6ac13b6
β€’
1 Parent(s): 416168a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -49
app.py CHANGED
@@ -1,8 +1,7 @@
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
  from fpdf import FPDF
4
- from docx import Document
5
- import os
6
 
7
  css = '''
8
  .gradio-container{max-width: 1000px !important}
@@ -24,29 +23,9 @@ def format_prompt(message, history, system_prompt=None):
24
  prompt += f"[INST] {message} [/INST]"
25
  return prompt
26
 
27
- def save_to_file(content, filename, format):
28
- try:
29
- if format == "pdf":
30
- pdf = FPDF()
31
- pdf.add_page()
32
- pdf.set_auto_page_break(auto=True, margin=15)
33
- pdf.set_font("Arial", size=12)
34
- pdf.multi_cell(0, 10, content)
35
- pdf.output(f"{filename}.pdf")
36
- elif format == "docx":
37
- doc = Document()
38
- doc.add_paragraph(content)
39
- doc.save(f"{filename}.docx")
40
- elif format == "txt":
41
- with open(f"{filename}.txt", 'w') as file:
42
- file.write(content)
43
- return f"File saved successfully as {filename}.{format}"
44
- except Exception as e:
45
- return f"Error saving file: {str(e)}"
46
-
47
  def generate(
48
  prompt, history, system_prompt=None, temperature=0.2, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0,
49
- save_format="txt", save_file=None
50
  ):
51
  temperature = float(temperature)
52
  if temperature < 1e-2:
@@ -70,32 +49,55 @@ def generate(
70
  for response in stream:
71
  output += response.token.text
72
  yield output
73
-
74
- if save_file:
75
- # Ensure filename is valid and doesn't contain invalid characters
76
- save_file = save_file.strip().replace(' ', '_')
77
- save_message = save_to_file(output, save_file, save_format)
78
- yield f"{output}\n\n{save_message}"
79
 
80
- with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
81
- gr.Markdown("# GRAB DOC")
82
- prompt = gr.Textbox(label="Prompt", placeholder="Enter your text here...", lines=4)
83
- history = gr.State([])
84
- system_prompt = gr.Textbox(label="System Prompt", placeholder="Enter system instructions (optional)...", lines=2, visible=False)
85
- temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, step=0.1, value=0.2)
86
- max_new_tokens = gr.Slider(label="Max New Tokens", minimum=1, maximum=1024, step=1, value=1024)
87
- top_p = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, step=0.05, value=0.95)
88
- repetition_penalty = gr.Slider(label="Repetition Penalty", minimum=0.0, maximum=2.0, step=0.1, value=1.0)
89
- save_format = gr.Dropdown(label="Save Format", choices=["txt", "pdf", "docx"], value="txt")
90
- save_file = gr.Textbox(label="Save File Name", placeholder="Enter the filename (without extension)...", lines=1)
91
-
92
- submit = gr.Button("Generate")
93
- output = gr.Textbox(label="Generated Output", lines=20)
94
-
95
- submit.click(
96
- generate,
97
- inputs=[prompt, history, system_prompt, temperature, max_new_tokens, top_p, repetition_penalty, save_format, save_file],
98
- outputs=output
99
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
  demo.queue().launch(show_api=False)
 
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
  from fpdf import FPDF
4
+ import docx
 
5
 
6
  css = '''
7
  .gradio-container{max-width: 1000px !important}
 
23
  prompt += f"[INST] {message} [/INST]"
24
  return prompt
25
 
26
+ # Generate text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def generate(
28
  prompt, history, system_prompt=None, temperature=0.2, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0,
 
29
  ):
30
  temperature = float(temperature)
31
  if temperature < 1e-2:
 
49
  for response in stream:
50
  output += response.token.text
51
  yield output
52
+ return output
 
 
 
 
 
53
 
54
+ # Save the generated content to a file
55
+ def save_file(content, filename, file_format):
56
+ if file_format == "pdf":
57
+ pdf = FPDF()
58
+ pdf.add_page()
59
+ pdf.set_auto_page_break(auto=True, margin=15)
60
+ pdf.set_font("Arial", size=12)
61
+ for line in content.split("\n"):
62
+ pdf.multi_cell(0, 10, line)
63
+ pdf.output(f"{filename}.pdf")
64
+ return f"{filename}.pdf"
65
+ elif file_format == "docx":
66
+ doc = docx.Document()
67
+ doc.add_paragraph(content)
68
+ doc.save(f"{filename}.docx")
69
+ return f"{filename}.docx"
70
+ elif file_format == "txt":
71
+ with open(f"{filename}.txt", "w") as f:
72
+ f.write(content)
73
+ return f"{filename}.txt"
74
+ else:
75
+ raise ValueError("Unsupported file format")
76
+
77
+ # Combine generate and save file functions
78
+ def generate_and_save(prompt, history, filename="output", file_format="pdf", system_prompt=None, temperature=0.2, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0):
79
+ generated_text = ""
80
+ for output in generate(prompt, history, system_prompt, temperature, max_new_tokens, top_p, repetition_penalty):
81
+ generated_text = output
82
+ saved_file = save_file(generated_text, filename, file_format)
83
+ return generated_text, saved_file
84
+
85
+ # Create Gradio interface
86
+ demo = gr.Interface(
87
+ fn=generate_and_save,
88
+ inputs=[
89
+ gr.Textbox(placeholder="Type your message here...", label="Prompt"),
90
+ gr.State(value=[]), # history
91
+ gr.Textbox(placeholder="Filename (default: output)", label="Filename", value="output"),
92
+ gr.Radio(["pdf", "docx", "txt"], label="File Format", value="pdf"),
93
+ ],
94
+ outputs=[
95
+ gr.Textbox(label="Generated Text"),
96
+ gr.File(label="Download File")
97
+ ],
98
+ css=css,
99
+ title="",
100
+ theme="bethecloud/storj_theme"
101
+ )
102
 
103
  demo.queue().launch(show_api=False)