Spaces:
Runtime error
Runtime error
Federico69
commited on
Commit
•
5b88f3d
1
Parent(s):
dec807c
Upload folder using huggingface_hub
Browse files
chatfp.py
CHANGED
@@ -1,27 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
import os
|
4 |
|
5 |
-
# Define la función que interactúa con ChatGPT-4
|
6 |
def chat_with_gpt4(prompt):
|
7 |
-
api_key = os.getenv("OPENAI_API_KEY")
|
8 |
if not api_key:
|
9 |
raise ValueError("API key not found. Please set up the OPENAI_API_KEY environment variable.")
|
10 |
-
|
11 |
headers = {
|
12 |
'Authorization': f'Bearer {api_key}',
|
13 |
'Content-Type': 'application/json',
|
14 |
}
|
|
|
15 |
data = {
|
16 |
-
'model': 'gpt-4',
|
17 |
-
'
|
18 |
-
'max_tokens': 150
|
19 |
}
|
|
|
20 |
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=data)
|
|
|
21 |
if response.status_code == 200:
|
22 |
return response.json()['choices'][0]['message']['content']
|
23 |
else:
|
24 |
-
return "Error en la API de OpenAI: " + response.
|
25 |
|
26 |
# Crea la interfaz de Gradio
|
27 |
interface = gr.Interface(
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
import os
|
4 |
|
|
|
5 |
def chat_with_gpt4(prompt):
|
6 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
7 |
if not api_key:
|
8 |
raise ValueError("API key not found. Please set up the OPENAI_API_KEY environment variable.")
|
9 |
+
|
10 |
headers = {
|
11 |
'Authorization': f'Bearer {api_key}',
|
12 |
'Content-Type': 'application/json',
|
13 |
}
|
14 |
+
|
15 |
data = {
|
16 |
+
'model': 'gpt-4',
|
17 |
+
'messages': [{'role': 'user', 'content': prompt}],
|
18 |
+
'max_tokens': 150
|
19 |
}
|
20 |
+
|
21 |
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=data)
|
22 |
+
|
23 |
if response.status_code == 200:
|
24 |
return response.json()['choices'][0]['message']['content']
|
25 |
else:
|
26 |
+
return "Error en la API de OpenAI: " + response.json().get('error', {}).get('message', 'Unknown error')
|
27 |
|
28 |
# Crea la interfaz de Gradio
|
29 |
interface = gr.Interface(
|