import requests import os import gradio as gr import openai # Define the LLM function def generacion_llm(texto_input): # Define the system and user messages formato_json = ''' { "nombre_usuario": " ", "ubicación_geográfica": " ", "descripción_reto": " ", "dudas_conceptuales": " ", "certezas_conceptuales": " ", "expectativas_del_taller": " " } ''' mensaje_sistema = ( "Eres un experto en identificar aspectos descriptivos de las razones " "por las cuales un usuario necesita asesoría para implementar retos " "que involucren inteligencia artificial de varios tipos." ) mensaje_usuario = ( f"Analizar el texto: \nTexto a Analizar: {texto_input}, que es una redacción \ libre de un usuario que busca asesoría para su uso óptimo de la inteligencia \ artificial, que busca orientación a través de un taller de asesoría de un facilitador, \ para lo cual te pido identifiques los siguientes extractos del texto \ en el formato JSON: {formato_json}\n Si no hubiera claridad suficiente sobre alguno \ de los contenidos del formato JSON, escribir 'No hay suficiente información'" ) version_model = 'gpt-3.5-turbo-0125' # Call OpenAI API try: response = client.chat.completions.create( model=version_model, messages=[ {"role": "system", "content": mensaje_sistema}, {"role": "user", "content": mensaje_usuario} ], temperature=0.8, max_tokens=300, top_p=1, ) # Extract the generated text from the response texto_respuesta = response.choices[0].message.content # Try parsing as JSON (if applicable) return texto_respuesta # Return plain text for now (replace with JSON if needed) except Exception as e: return f"Error: {e}" def guardar_en_airtable(nombre, email, json_generado): if not nombre or not email: return "Todos los campos (nombre, email, imagen) son obligatorios." url = f"https://api.airtable.com/v0/{BASE_ID}/{TABLE_NAME}" print(url) headers = { "Authorization": f"Bearer {AIRTABLE_API_KEY}", "Content-Type": "application/json" } data = { "fields": { "Nombre": nombre, "Email": email, "json_HF": json_generado } } try: response_out = requests.get(url, headers=headers) print(response_out.json()) response = requests.post(url, headers=headers, json=data) if response.status_code == 200 or response.status_code == 201: return "Información guardada en Airtable exitosamente." else: return f"Error guardando en Airtable: {response.text}" except Exception as e: return f"Error de conexión: {e}"