Spaces:
Sleeping
Sleeping
Commit
·
c012ec0
1
Parent(s):
9dc66e6
update
Browse files
app.py
CHANGED
@@ -4,16 +4,19 @@ import os
|
|
4 |
|
5 |
# Set up OpenAI API key (use an environment variable for security)
|
6 |
api_key = os.getenv("OPENAI_API_KEY")
|
7 |
-
|
|
|
|
|
|
|
8 |
|
9 |
def chat_with_ai(prompt):
|
10 |
"""Generates a response from OpenAI's GPT-3.5."""
|
11 |
try:
|
12 |
-
response =
|
13 |
model="gpt-3.5-turbo",
|
14 |
messages=[{"role": "user", "content": prompt}]
|
15 |
)
|
16 |
-
return response
|
17 |
except Exception as e:
|
18 |
return f"Error: {str(e)}"
|
19 |
|
@@ -26,4 +29,4 @@ iface = gr.Interface(
|
|
26 |
|
27 |
# Launch the app
|
28 |
if __name__ == "__main__":
|
29 |
-
iface.launch()
|
|
|
4 |
|
5 |
# Set up OpenAI API key (use an environment variable for security)
|
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 |
+
openai.api_key = api_key # Set the API key globally
|
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",
|
17 |
messages=[{"role": "user", "content": prompt}]
|
18 |
)
|
19 |
+
return response["choices"][0]["message"]["content"]
|
20 |
except Exception as e:
|
21 |
return f"Error: {str(e)}"
|
22 |
|
|
|
29 |
|
30 |
# Launch the app
|
31 |
if __name__ == "__main__":
|
32 |
+
iface.launch()
|