Spaces:
Running
Running
File size: 4,256 Bytes
ad773e5 edb2d41 ad773e5 edb2d41 ad773e5 edb2d41 ad773e5 edb2d41 fec8e6e edb2d41 ad773e5 1141e03 ad773e5 edb2d41 4c7e567 edb2d41 4c7e567 edb2d41 4c7e567 edb2d41 fec8e6e edb2d41 d9bd1ee 4c7e567 edb2d41 4c7e567 edb2d41 a48362a edb2d41 1141e03 edb2d41 a48362a 4c7e567 edb2d41 1141e03 4c7e567 edb2d41 4c7e567 edb2d41 4c7e567 edb2d41 4c7e567 edb2d41 4bfc4dc edb2d41 4c7e567 edb2d41 4bfc4dc edb2d41 4bfc4dc edb2d41 ad773e5 edb2d41 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import gradio as gr
from openai import OpenAI
import os
from io import BytesIO
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from docx import Document
# Custom CSS
css = '''
.gradio-container{max-width: 1000px !important}
h1{text-align:center}
footer {
visibility: hidden
}
'''
# Set up OpenAI client
ACCESS_TOKEN = os.getenv("HF_TOKEN")
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1/",
api_key=ACCESS_TOKEN,
)
# Function to handle chat responses
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-8B-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
# Function to save chat history to a text file
def save_as_txt(history):
file_path = "chat_history.txt"
with open(file_path, "w") as f:
for user_message, assistant_message in history:
f.write(f"User: {user_message}\n")
f.write(f"Assistant: {assistant_message}\n")
return file_path
# Function to save chat history to a DOCX file
def save_as_docx(history):
file_path = "chat_history.docx"
doc = Document()
doc.add_heading('Chat History', 0)
for user_message, assistant_message in history:
doc.add_paragraph(f"User: {user_message}")
doc.add_paragraph(f"Assistant: {assistant_message}")
doc.save(file_path)
return file_path
# Function to save chat history to a PDF file
def save_as_pdf(history):
file_path = "chat_history.pdf"
buffer = BytesIO()
c = canvas.Canvas(buffer, pagesize=letter)
width, height = letter
y = height - 40
c.drawString(30, y, "Chat History")
y -= 30
for user_message, assistant_message in history:
c.drawString(30, y, f"User: {user_message}")
y -= 20
c.drawString(30, y, f"Assistant: {assistant_message}")
y -= 30
if y < 40:
c.showPage()
y = height - 40
c.save()
buffer.seek(0)
with open(file_path, "wb") as f:
f.write(buffer.read())
return file_path
# Function to handle file saving based on format
def handle_file_save(history, file_format):
if file_format == "txt":
return save_as_txt(history)
elif file_format == "docx":
return save_as_docx(history)
elif file_format == "pdf":
return save_as_pdf(history)
return None
# Handler function for Gradio app
def save_handler(message, history, system_message, max_tokens, temperature, top_p, file_format):
new_history = history + [(message, next(respond(message, history, system_message, max_tokens, temperature, top_p)))]
saved_file = handle_file_save(new_history, file_format)
return saved_file, new_history
# Gradio interface
demo = gr.Interface(
fn=save_handler,
inputs=[
gr.Textbox(value="", label="Message"),
gr.State([]), # Initialize state as an empty list
gr.Textbox(value="", label="System message"),
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"),
gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.95,
step=0.05,
label="Top-P",
),
gr.Dropdown(
choices=["txt", "docx", "pdf"],
label="Save as",
),
],
outputs=[gr.File(label="Download Chat History"), gr.State()],
css=css,
theme="allenai/gradio-theme",
)
if __name__ == "__main__":
demo.launch() |