Spaces:
Sleeping
Sleeping
import gradio as gr | |
from openai import OpenAI | |
import os | |
from fpdf import FPDF | |
import docx | |
# Fetching the access token from environment variables | |
ACCESS_TOKEN = os.getenv("HF_TOKEN") | |
# Initialize the OpenAI client | |
client = OpenAI( | |
base_url="https://api-inference.huggingface.co/v1/", | |
api_key=ACCESS_TOKEN, | |
) | |
css = ''' | |
.gradio-container{max-width: 1000px !important} | |
h1{text-align:center} | |
footer { | |
visibility: hidden | |
} | |
''' | |
# Function to format and respond to the user's message using the Llama model | |
def respond( | |
message, | |
history: list[tuple[str, str]], | |
system_message, | |
max_tokens, | |
temperature, | |
top_p, | |
): | |
messages = [{"role": "system", "content": system_message}] | |
for val in history: | |
if val[0]: | |
messages.append({"role": "user", "content": val[0]}) | |
if val[1]: | |
messages.append({"role": "assistant", "content": val[1]}) | |
messages.append({"role": "user", "content": message}) | |
response = "" | |
for message in client.chat.completions.create( | |
model="meta-llama/Meta-Llama-3.1-70B-Instruct", | |
max_tokens=max_tokens, | |
stream=True, | |
temperature=temperature, | |
top_p=top_p, | |
messages=messages, | |
): | |
token = message.choices[0].delta.content | |
response += token | |
yield response | |
# Save the generated content to a file with the specified font, font size, and line spacing | |
def save_file(content, filename, file_format, font_name, font_size, line_spacing): | |
font_path = f"font/{font_name}" | |
if file_format == "pdf": | |
pdf = FPDF() | |
pdf.add_page() | |
pdf.set_auto_page_break(auto=True, margin=15) | |
pdf.add_font("CustomFont", "", font_path, uni=True) | |
pdf.set_font("CustomFont", size=int(font_size)) | |
for line in content.split("\n"): | |
pdf.multi_cell(0, 10 * float(line_spacing), line) | |
pdf.output(f"{filename}.pdf") | |
return f"{filename}.pdf" | |
elif file_format == "docx": | |
doc = docx.Document() | |
paragraph = doc.add_paragraph(content) | |
run = paragraph.runs[0] | |
run.font.name = font_name.split(".")[0] # Use the font name without extension | |
run.font.size = docx.shared.Pt(int(font_size)) # Set the font size | |
paragraph_format = paragraph.paragraph_format | |
paragraph_format.line_spacing = float(line_spacing) # Set the line spacing | |
doc.save(f"{filename}.docx") | |
return f"{filename}.docx" | |
elif file_format == "txt": | |
with open(f"{filename}.txt", "w") as f: | |
f.write(content) | |
return f"{filename}.txt" | |
else: | |
raise ValueError("Unsupported file format") | |
# Combine respond and save file functions | |
def respond_and_save(message, history, system_message, filename="output", file_format="pdf", font_name="arial.ttf", font_size="18", line_spacing="1.5", max_tokens=512, temperature=0.7, top_p=0.95): | |
generated_text = "" | |
for output in respond(message, history, system_message, max_tokens, temperature, top_p): | |
generated_text = output | |
saved_file = save_file(generated_text, filename, file_format, font_name, font_size, line_spacing) | |
return generated_text, history + [(message, generated_text)], saved_file | |
# Create the font dropdown | |
font_choice = gr.Dropdown( | |
choices=[ | |
"DejaVuMathTeXGyre.ttf", | |
"FiraCode-Medium.ttf", | |
"InputMono-Light.ttf", | |
"JetBrainsMono-Thin.ttf", | |
"ProggyCrossed Regular Mac.ttf", | |
"SourceCodePro-Black.ttf", | |
"arial.ttf", | |
"calibri.ttf", | |
"mukta-malar-extralight.ttf", | |
"noto-sans-arabic-medium.ttf", | |
"times new roman.ttf", | |
"ANGSA.ttf", | |
"Book-Antiqua.ttf", | |
"CONSOLA.TTF", | |
"COOPBL.TTF", | |
"Rockwell-Bold.ttf", | |
"Candara Light.TTF", | |
"Carlito-Regular.ttf", | |
"Castellar.ttf", | |
"Courier New.ttf", | |
"LSANS.TTF", | |
"Lucida Bright Regular.ttf", | |
"TRTempusSansITC.ttf", | |
"Verdana.ttf", | |
"bell-mt.ttf", | |
"eras-itc-light.ttf", | |
"fonnts.com-aptos-light.ttf", | |
"georgia.ttf", | |
"segoeuithis.ttf", | |
"youyuan.TTF", | |
"TfPonetoneExpanded-7BJZA.ttf", | |
], | |
value="arial.ttf", | |
label="Font Style" | |
) | |
# Create the font size dropdown | |
font_size = gr.Dropdown( | |
choices=["12", "14", "16", "18", "20", "22", "24"], | |
value="18", | |
label="Font Size" | |
) | |
# Create the line spacing dropdown | |
line_spacing = gr.Dropdown( | |
choices=[1.0, 1.15, 1.5, 2.0, 2.5, 3.0], | |
value=1.5, | |
label="Line Spacing" | |
) | |
# Create Gradio interface | |
demo = gr.Interface( | |
fn=respond_and_save, | |
inputs=[ | |
gr.Textbox(placeholder="Type your message here...", label="Chatbot", lines=5), | |
gr.State(value=[]), | |
gr.Textbox(placeholder="System message", label="System message", value="", visible=False), | |
gr.Textbox(placeholder="Filename (default: output)", label="Filename", value="output"), | |
gr.Radio(["pdf", "docx", "txt"], label="File Format", value="pdf"), | |
font_choice, | |
font_size, # Add the font size dropdown to the interface | |
line_spacing, # Add the line spacing dropdown to the interface | |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature", visible=False), | |
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P", visible=False), | |
], | |
outputs=[ | |
gr.Textbox(label="Generated Text", lines=5), | |
gr.State(value=[]), | |
gr.File(label="Download File") | |
], | |
css=css, | |
title="GRABDOC PRO", | |
theme="bethecloud/storj_theme" | |
) | |
demo.queue().launch(show_api=False) |