Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
from huggingface_hub import InferenceClient
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
css = '''
|
5 |
.gradio-container{max-width: 1000px !important}
|
@@ -10,6 +12,7 @@ footer {
|
|
10 |
'''
|
11 |
|
12 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
|
|
13 |
def format_prompt(message, history, system_prompt=None):
|
14 |
prompt = "<s>"
|
15 |
for user_prompt, bot_response in history:
|
@@ -19,7 +22,8 @@ def format_prompt(message, history, system_prompt=None):
|
|
19 |
prompt += f"[SYS] {system_prompt} [/SYS]"
|
20 |
prompt += f"[INST] {message} [/INST]"
|
21 |
return prompt
|
22 |
-
|
|
|
23 |
def generate(
|
24 |
prompt, history, system_prompt=None, temperature=0.2, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0,
|
25 |
):
|
@@ -47,9 +51,50 @@ def generate(
|
|
47 |
yield output
|
48 |
return output
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
css=css,
|
54 |
title="",
|
55 |
theme="bethecloud/storj_theme"
|
|
|
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}
|
|
|
12 |
'''
|
13 |
|
14 |
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
|
15 |
+
|
16 |
def format_prompt(message, history, system_prompt=None):
|
17 |
prompt = "<s>"
|
18 |
for user_prompt, bot_response in history:
|
|
|
22 |
prompt += f"[SYS] {system_prompt} [/SYS]"
|
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 |
):
|
|
|
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"
|