|
|
import gradio as gr |
|
|
import openai |
|
|
import os |
|
|
|
|
|
|
|
|
openai.api_key = os.getenv("OPENAI_KEY") |
|
|
|
|
|
|
|
|
def generacion_llm(texto_input): |
|
|
|
|
|
formato_json = ''' |
|
|
{ |
|
|
"reto": " ", |
|
|
"dudas": " ", |
|
|
"preguntas": " ", |
|
|
"expectativas": " " |
|
|
} |
|
|
''' |
|
|
|
|
|
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 mostrado al final, buscando identificar los siguientes " |
|
|
f"extractos en el formato JSON: {formato_json}\n\nTexto a Analizar: {texto_input}" |
|
|
) |
|
|
|
|
|
|
|
|
try: |
|
|
response = openai.ChatCompletion.create( |
|
|
model="gpt-3.5-turbo", |
|
|
messages=[ |
|
|
{"role": "system", "content": mensaje_sistema}, |
|
|
{"role": "user", "content": mensaje_usuario} |
|
|
], |
|
|
temperature=0.8, |
|
|
max_tokens=300, |
|
|
top_p=1, |
|
|
frequency_penalty=0, |
|
|
presence_penalty=0 |
|
|
) |
|
|
|
|
|
|
|
|
texto_respuesta = response["choices"][0]["message"]["content"] |
|
|
|
|
|
|
|
|
return texto_respuesta |
|
|
except Exception as e: |
|
|
return f"Error: {e}" |
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=generacion_llm, |
|
|
inputs=gr.Textbox(label="Ingrese su texto para analizar"), |
|
|
outputs=gr.Textbox(label="Resultado JSON"), |
|
|
title="Extractor de Texto a JSON", |
|
|
description="Ingrese el texto para analizar y extraer informaci贸n en un formato JSON predefinido." |
|
|
) |
|
|
|
|
|
interface.launch() |
|
|
|
|
|
|
|
|
|
|
|
''' |
|
|
import gradio as gr |
|
|
import json |
|
|
import os |
|
|
from openai import OpenAI |
|
|
|
|
|
api_key = os.getenv("OPENAI_KEY") |
|
|
client = OpenAI(api_key=api_key) |
|
|
modelx = 'gpt-3.5-turbo-0125' |
|
|
|
|
|
def generacion_llm(texto_input): |
|
|
# Define the system and user messages |
|
|
formato_json = ''' |
|
|
{ |
|
|
"reto": " ", |
|
|
"dudas": " ", |
|
|
"preguntas": " ", |
|
|
"expectativas": " " |
|
|
} |
|
|
''' |
|
|
|
|
|
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 mostrado al final, buscando identificar los siguientes " |
|
|
f"extractos en el formato JSON: {formato_json}\n\nTexto a Analizar: {texto_input}" |
|
|
) |
|
|
|
|
|
# Call OpenAI API |
|
|
response = openai.ChatCompletion.create( |
|
|
model=modelx, |
|
|
messages=[ |
|
|
{"role": "system", "content": mensaje_sistema}, |
|
|
{"role": "user", "content": mensaje_usuario} |
|
|
], |
|
|
temperature=0.8, |
|
|
max_tokens=300, |
|
|
top_p=1, |
|
|
frequency_penalty=0, |
|
|
presence_penalty=0 |
|
|
) |
|
|
|
|
|
# Extract the generated text from the response |
|
|
texto_respuesta = response["choices"][0]["message"]["content"] |
|
|
|
|
|
return texto_respuesta |
|
|
|
|
|
# Define Gradio app |
|
|
interface = gr.Interface( |
|
|
fn=generacion_llm, |
|
|
inputs=gr.Textbox(label="Ingrese su texto para analizar"), |
|
|
outputs=gr.Textbox(label="Resultado JSON"), |
|
|
title="Extractor de Texto a JSON", |
|
|
description="Ingrese el texto para analizar y extraer informaci贸n en un formato JSON predefinido." |
|
|
) |
|
|
|
|
|
interface.launch() |
|
|
''' |