File size: 1,947 Bytes
68103f4
6da4e96
 
d31531c
 
30aa673
d31531c
6da4e96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13b8afd
 
6da4e96
 
 
 
 
 
 
13b8afd
6da4e96
13b8afd
6da4e96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9d7c9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import gradio as gr
import openai
import os
import openai
from openai import OpenAI
api_key = os.environ("OPENAI_KEY")
client = OpenAI(api_key=api_key)

# Define the LLM function
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
    try:
        response = client.chat.completions.create(
            model=modelox,
            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}"

# 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()