Spaces:
Sleeping
Sleeping
prithivMLmods
commited on
Commit
β’
416168a
1
Parent(s):
310fc8c
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ from huggingface_hub import InferenceClient
|
|
2 |
import gradio as gr
|
3 |
from fpdf import FPDF
|
4 |
from docx import Document
|
|
|
5 |
|
6 |
css = '''
|
7 |
.gradio-container{max-width: 1000px !important}
|
@@ -24,20 +25,24 @@ def format_prompt(message, history, system_prompt=None):
|
|
24 |
return prompt
|
25 |
|
26 |
def save_to_file(content, filename, format):
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
42 |
def generate(
|
43 |
prompt, history, system_prompt=None, temperature=0.2, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0,
|
@@ -66,10 +71,11 @@ def generate(
|
|
66 |
output += response.token.text
|
67 |
yield output
|
68 |
|
69 |
-
# Save content to file
|
70 |
if save_file:
|
71 |
-
|
72 |
-
|
|
|
|
|
73 |
|
74 |
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
75 |
gr.Markdown("# GRAB DOC")
|
|
|
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}
|
|
|
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,
|
|
|
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")
|