Spaces:
Sleeping
Sleeping
Commit
·
0e66e48
1
Parent(s):
c012ec0
update
Browse files
app.py
CHANGED
|
@@ -2,15 +2,19 @@ import gradio as gr
|
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Set up OpenAI API key
|
| 6 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
-
if not api_key:
|
| 8 |
-
raise ValueError("OPENAI_API_KEY environment variable is not set")
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def chat_with_ai(prompt):
|
| 13 |
"""Generates a response from OpenAI's GPT-3.5."""
|
|
|
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
response = openai.ChatCompletion.create(
|
| 16 |
model="gpt-3.5-turbo",
|
|
@@ -29,4 +33,4 @@ iface = gr.Interface(
|
|
| 29 |
|
| 30 |
# Launch the app
|
| 31 |
if __name__ == "__main__":
|
| 32 |
-
iface.launch()
|
|
|
|
| 2 |
import openai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Set up OpenAI API key
|
| 6 |
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
if not api_key:
|
| 9 |
+
print("Warning: OPENAI_API_KEY is not set. Please add it in your Hugging Face Space secrets.")
|
| 10 |
+
else:
|
| 11 |
+
openai.api_key = api_key # Set the API key globally
|
| 12 |
|
| 13 |
def chat_with_ai(prompt):
|
| 14 |
"""Generates a response from OpenAI's GPT-3.5."""
|
| 15 |
+
if not api_key:
|
| 16 |
+
return "Error: Missing API key. Please configure it in the Hugging Face Space settings."
|
| 17 |
+
|
| 18 |
try:
|
| 19 |
response = openai.ChatCompletion.create(
|
| 20 |
model="gpt-3.5-turbo",
|
|
|
|
| 33 |
|
| 34 |
# Launch the app
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
iface.launch()
|