first.appli / app.py
Narchethan's picture
mise a jour
b8f5bef
raw
history blame
2.07 kB
import openai
import gradio as gr
def openai_chat(api_key, system_prompt, user_prompt, temperature, max_tokens, top_p):
try:
# Set the OpenAI API key
openai.api_key = api_key
# Generate the completion
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
],
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
)
# Extract the assistant's reply
return response.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"
# Define the Gradio interface
with gr.Blocks() as demo:
gr.Markdown("""# history prof
it is my first appli. i am bad in history but to help me for my homework.
""")
api_key_input = gr.Textbox(label="OpenAI API Key", placeholder="Enter your OpenAI API key", type="password")
system_prompt_input = gr.Textbox(label="ethan's history prof", value="You are a history professor 5e in FRENCH.", placeholder="You are a history professor 5e in FRENCH.")
user_prompt_input = gr.Textbox(label="user chat", placeholder="metez votre question ici.")
temperature_slider = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.7, step=0.1)
max_tokens_slider = gr.Slider(label="Max Tokens", minimum=10, maximum=1024, value=100, step=10)
top_p_slider = gr.Slider(label="Top P", minimum=0.1, maximum=1.0, value=1.0, step=0.1)
output = gr.Textbox(label="history prof πŸ‘¨β€πŸ« ")
generate_button = gr.Button("πŸ“–")
generate_button.click(
openai_chat,
inputs=[
api_key_input,
system_prompt_input,
user_prompt_input,
temperature_slider,
max_tokens_slider,
top_p_slider
],
outputs=[output]
)
if __name__ == "__main__":
demo.launch()
#Merci papa de m'avoir offert cet ordi ❀️❀️❀️