File size: 1,147 Bytes
21bbf4f 62a926f 21bbf4f e624acf 21bbf4f e624acf 21bbf4f e624acf 21bbf4f 62a926f 21bbf4f 45aaebd 21bbf4f 45aaebd 62a926f 21bbf4f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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()
|