Spaces:
Sleeping
Sleeping
Commit
·
af14490
1
Parent(s):
3bfc696
1 billionth update
Browse files
app.py
CHANGED
@@ -1,21 +1,23 @@
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
|
|
|
|
4 |
from dotenv import load_dotenv
|
|
|
5 |
|
6 |
-
# Set up OpenAI API key
|
7 |
api_key = os.getenv("OPENAI_API_KEY")
|
8 |
-
|
9 |
if not api_key:
|
10 |
-
print("Warning: OPENAI_API_KEY is not set. Please add it in
|
11 |
else:
|
|
|
12 |
openai.api_key = api_key # Set the API key globally
|
13 |
|
14 |
def chat_with_ai(prompt):
|
15 |
"""Generates a response from OpenAI's GPT-3.5."""
|
16 |
if not api_key:
|
17 |
return "Error: Missing API key. Please configure it in the Hugging Face Space settings."
|
18 |
-
|
19 |
try:
|
20 |
response = openai.ChatCompletion.create(
|
21 |
model="gpt-3.5-turbo",
|
@@ -34,4 +36,4 @@ iface = gr.Interface(
|
|
34 |
|
35 |
# Launch the app
|
36 |
if __name__ == "__main__":
|
37 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
import os
|
4 |
+
|
5 |
+
# Load environment variables manually
|
6 |
from dotenv import load_dotenv
|
7 |
+
load_dotenv()
|
8 |
|
9 |
+
# Set up OpenAI API key (use an environment variable for security)
|
10 |
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
11 |
if not api_key:
|
12 |
+
print("Warning: OPENAI_API_KEY is not set. Please add it in Hugging Face Space secrets.")
|
13 |
else:
|
14 |
+
print(f"API Key detected, length: {len(api_key)}")
|
15 |
openai.api_key = api_key # Set the API key globally
|
16 |
|
17 |
def chat_with_ai(prompt):
|
18 |
"""Generates a response from OpenAI's GPT-3.5."""
|
19 |
if not api_key:
|
20 |
return "Error: Missing API key. Please configure it in the Hugging Face Space settings."
|
|
|
21 |
try:
|
22 |
response = openai.ChatCompletion.create(
|
23 |
model="gpt-3.5-turbo",
|
|
|
36 |
|
37 |
# Launch the app
|
38 |
if __name__ == "__main__":
|
39 |
+
iface.launch()
|