Spaces:
Sleeping
Sleeping
| from openai import OpenAI | |
| import gradio as gr | |
| def generate_response(message, history, api_key_input): | |
| if not api_key_input: | |
| return "Please enter an API key to initialize the OpenAI API." | |
| client = OpenAI(api_key=api_key_input) | |
| formatted_history = [] | |
| for msg in history: | |
| formatted_history.append({"role": msg["role"], "content": msg["content"]}) | |
| formatted_history.append({"role": "user", "content": message}) | |
| print("DEBUG formatted_history", formatted_history) | |
| response = client.chat.completions.create( | |
| model="gpt-3.5-turbo", messages=formatted_history, temperature=1.0 | |
| ) | |
| client.close() | |
| return response.choices[0].message.content | |
| with gr.Blocks() as demo: | |
| api_key_input = gr.Textbox( | |
| label="Enter API Key", type="password", placeholder="Enter your OpenAI API key" | |
| ) | |
| chatbot = gr.Chatbot(height=300, type="messages") | |
| gr.ChatInterface( | |
| fn=generate_response, | |
| type="messages", | |
| additional_inputs=api_key_input, | |
| chatbot=chatbot, | |
| ) | |
| demo.launch() | |