chatfp / chatfp.py
Federico69's picture
Upload folder using huggingface_hub
dec807c verified
raw
history blame
No virus
1.36 kB
import gradio as gr
import requests
import os # Importa os para acceder a variables de entorno
# Define la función que interactúa con ChatGPT-4
def chat_with_gpt4(prompt):
api_key = os.getenv("OPENAI_API_KEY") # Obtén la clave API desde los secretos configurados
if not api_key:
raise ValueError("API key not found. Please set up the OPENAI_API_KEY environment variable.")
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json',
}
data = {
'model': 'gpt-4', # Asegúrate de utilizar el modelo correcto de GPT
'prompt': prompt,
'max_tokens': 150 # Puedes ajustar el número de tokens según necesites
}
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=data)
if response.status_code == 200:
return response.json()['choices'][0]['message']['content']
else:
return "Error en la API de OpenAI: " + response.text
# Crea la interfaz de Gradio
interface = gr.Interface(
fn=chat_with_gpt4,
inputs=gr.Textbox(lines=2, placeholder="Escribe aquí tu pregunta para GPT-4..."),
outputs='text',
title="Chatbot con ChatGPT-4",
description="Este chatbot utiliza ChatGPT-4 para responder a tus preguntas. ¡Pruébalo!"
)
if __name__ == "__main__":
interface.launch()