Spaces:
Build error
Build error
| import os | |
| import openai | |
| import gradio as gr | |
| # Retrieve OpenAI API key from Hugging Face Secrets | |
| openai_api_key = os.getenv("OPENAI_API_KEY") | |
| def chatbot(user_input, history=[]): | |
| if not openai_api_key: | |
| return "⚠️ API key is missing. Please configure it in Hugging Face Secrets.", history | |
| history.append({"role": "user", "content": user_input}) | |
| try: | |
| response = openai.ChatCompletion.create( | |
| model="gpt-4o", | |
| messages=history, | |
| temperature=0.7, | |
| max_tokens=200, | |
| top_p=1, | |
| api_key=openai_api_key # Explicitly passing API key | |
| ) | |
| bot_reply = response["choices"][0]["message"]["content"] | |
| history.append({"role": "assistant", "content": bot_reply}) | |
| except Exception as e: | |
| bot_reply = f"❌ Error: {str(e)}" | |
| return bot_reply, history | |
| # Gradio Interface (Removing invalid keyword arguments) | |
| chatbot_ui = gr.ChatInterface( | |
| fn=chatbot, | |
| title="AI Chatbot", | |
| description="A simple chatbot powered by GPT-4o.", | |
| theme="soft" | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| chatbot_ui.launch() | |