Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,6 @@ import gradio as gr
|
|
| 5 |
# Retrieve OpenAI API key from Hugging Face Secrets
|
| 6 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
| 8 |
-
# Initialize OpenAI client
|
| 9 |
-
client = openai.OpenAI(api_key=openai_api_key)
|
| 10 |
-
|
| 11 |
-
# Chatbot function
|
| 12 |
def chatbot(user_input, history=[]):
|
| 13 |
if not openai_api_key:
|
| 14 |
return "⚠️ API key is missing. Please configure it in Hugging Face Secrets.", history
|
|
@@ -16,15 +12,16 @@ def chatbot(user_input, history=[]):
|
|
| 16 |
history.append({"role": "user", "content": user_input})
|
| 17 |
|
| 18 |
try:
|
| 19 |
-
response =
|
| 20 |
model="gpt-4o",
|
| 21 |
messages=history,
|
| 22 |
temperature=0.7,
|
| 23 |
max_tokens=200,
|
| 24 |
-
top_p=1
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
-
bot_reply = response
|
| 28 |
history.append({"role": "assistant", "content": bot_reply})
|
| 29 |
|
| 30 |
except Exception as e:
|
|
|
|
| 5 |
# Retrieve OpenAI API key from Hugging Face Secrets
|
| 6 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def chatbot(user_input, history=[]):
|
| 9 |
if not openai_api_key:
|
| 10 |
return "⚠️ API key is missing. Please configure it in Hugging Face Secrets.", history
|
|
|
|
| 12 |
history.append({"role": "user", "content": user_input})
|
| 13 |
|
| 14 |
try:
|
| 15 |
+
response = openai.ChatCompletion.create(
|
| 16 |
model="gpt-4o",
|
| 17 |
messages=history,
|
| 18 |
temperature=0.7,
|
| 19 |
max_tokens=200,
|
| 20 |
+
top_p=1,
|
| 21 |
+
api_key=openai_api_key # Explicitly passing API key
|
| 22 |
)
|
| 23 |
|
| 24 |
+
bot_reply = response["choices"][0]["message"]["content"]
|
| 25 |
history.append({"role": "assistant", "content": bot_reply})
|
| 26 |
|
| 27 |
except Exception as e:
|