qwen2.5-7b-inst / app.py
Rijgersberg's picture
Update app.py
5e429c1 verified
raw
history blame
3.29 kB
import os
from openai import OpenAI
import gradio as gr
api_key = os.environ.get('OPENAI_API_KEY')
client = OpenAI(api_key=api_key)
MODELS = [
'gpt-4o',
'gpt-4o-mini',
'gpt-4',
'gpt-4-turbo',
'gpt-3.5-turbo',
]
def generate(message, history, model, system_prompt,
temperature=1.0, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0):
history_openai_format = [{"role": "system", "content": system_prompt}]
for user, assistant in history:
history_openai_format.append({"role": "user", "content": user})
history_openai_format.append({"role": "assistant", "content": assistant})
history_openai_format.append({"role": "user", "content": message})
response = client.chat.completions.create(model=model,
messages=history_openai_format,
temperature=temperature,
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty,
stream=True)
partial_message = ""
for chunk in response:
if chunk.choices and chunk.choices[0].delta.content is not None:
partial_message += chunk.choices[0].delta.content
yield partial_message
chat_interface = gr.ChatInterface(
title='๐Ÿ’ฌ Private ChatGPT',
description='Chat with OpenAI models using their official API. OpenAI <a href="https://platform.openai.com/docs/concepts">promises</a> not to train on input or output of API calls.',
fn=generate,
analytics_enabled=False,
chatbot=gr.Chatbot(
show_label=False,
show_copy_button=True,
scale=1),
additional_inputs=[
gr.Dropdown(label="Model",
choices=MODELS,
value=MODELS[0],
allow_custom_value=False),
gr.Textbox(label="System prompt",
value="Je bent een slimme, behulpzame assistent van Edwin Rijgersberg"),
gr.Slider(label="Temperature",
minimum=0.,
maximum=2.0,
step=0.05,
value=1.0),
gr.Slider(label="Top P",
minimum=0.,
maximum=1.0,
step=0.05,
value=1.0),
gr.Slider(label="Frequency penalty",
minimum=0.,
maximum=1.0,
step=0.05,
value=0.),
gr.Slider(label="Presence penalty",
minimum=0.,
maximum=1.0,
step=0.05,
value=0.),
],
textbox=gr.Textbox(container=False,
show_label=False,
label="Message",
placeholder="Type een bericht...",
scale=7),
additional_inputs_accordion=gr.Accordion(label="Instellingen", open=False),
show_progress="full",
submit_btn="Genereer",
stop_btn="Stop",
retry_btn="๐Ÿ”„ Opnieuw",
undo_btn="โ†ฉ๏ธ Ongedaan maken",
clear_btn="๐Ÿ—‘๏ธ Wissen",
)
chat_interface.launch(share=True)