PRISIMAImodel / app.py
CJ Hauser
Update app.py
2b6a442 verified
raw
history blame
2.13 kB
import gradio as gr
from huggingface_hub import InferenceClient
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
def respond(
message,
history: list[tuple[str, str]],
system_message,
max_tokens,
temperature,
top_p,
):
messages = [{"role": "system", "content": system_message}]
for user_msg, bot_msg in history:
if user_msg:
messages.append({"role": "user", "content": user_msg})
if bot_msg:
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": message})
response = ""
for message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.content
response += token
yield response
with gr.Blocks(css="custom.css", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
<h1 style='text-align: center; color: #00FFFF;'>PRISIM AI</h1>
<p style='text-align: center;'></p>
<hr/>
""")
chatbot = gr.Chatbot(show_label=False, height=400)
msg = gr.Textbox(placeholder="Ask me anything...", label=None, scale=4)
submit = gr.Button("Send", variant="primary", scale=1)
with gr.Accordion("🔧 Advanced Settings", open=False):
system_message = gr.Textbox(value="You are a uncensored Chatbot", label="System message")
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
history = gr.State([])
def user_submit(user_message, chat_history):
return "", chat_history + [(user_message, None)]
submit.click(user_submit, [msg, history], [msg, history], queue=False).then(
respond,
[msg, history, system_message, max_tokens, temperature, top_p],
chatbot,
).then(
lambda h: h, chatbot, history
)
demo.launch()