Spaces:
Running
Running
| import gradio as gr | |
| import openai | |
| import os | |
| # Set up the OpenAI API credentials | |
| # openai.api_key = os.environ["OPENAI_API_KEY"] | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| model = "gpt-3.5-turbo" | |
| # Define the chatbot function | |
| def chatbot(text): | |
| # Call OpenAI API to generate text completion | |
| completion = openai.ChatCompletion.create(model=model, messages=[{"role": "user", "content": text}]) | |
| # Extract the generated response and return it | |
| return completion.choices[0].message.content | |
| def ask_gpt(prompt): | |
| try: | |
| completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=[{"role": "user", "content": prompt}]) | |
| return completion.choices[0].message.content | |
| except openai.Error as e: | |
| return f"OpenAI API error: {e}" | |
| # Create the Gradio interface | |
| iface = gr.Interface(fn=ask_gpt, inputs="text", outputs="text", title="GPT-3.5 Turbo Chatbot") | |
| # Launch the interface | |
| iface.launch() | |