jcmachicao commited on
Commit
68103f4
verified
1 Parent(s): b38660b

Create app_new.py

Browse files
Files changed (1) hide show
  1. app_new.py +75 -0
app_new.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import json
4
+ import os
5
+ from openai import OpenAI
6
+
7
+ api_key = os.getenv("OPENAI_KEY")
8
+ client = OpenAI(api_key=api_key)
9
+ modelx = 'gpt-3.5-turbo-0125'
10
+
11
+ def generacion_llm(texto_input):
12
+ # Define the system and user messages
13
+ formato_json = '''
14
+ {
15
+ "reto": " ",
16
+ "dudas": " ",
17
+ "preguntas": " ",
18
+ "expectativas": " "
19
+ }
20
+ '''
21
+
22
+ mensaje_sistema = (
23
+ "Eres un experto en identificar aspectos descriptivos de las razones "
24
+ "por las cuales un usuario necesita asesor铆a para implementar retos "
25
+ "que involucren inteligencia artificial de varios tipos."
26
+ )
27
+
28
+ mensaje_usuario = (
29
+ f"Analizar el texto mostrado al final, buscando identificar los siguientes "
30
+ f"extractos en el formato JSON: {formato_json}\n\nTexto a Analizar: {texto_input}"
31
+ )
32
+
33
+ # Call OpenAI API
34
+ response = openai.ChatCompletion.create(
35
+ model=modelx,
36
+ messages=[
37
+ {"role": "system", "content": mensaje_sistema},
38
+ {"role": "user", "content": mensaje_usuario}
39
+ ],
40
+ temperature=0.8,
41
+ max_tokens=300,
42
+ top_p=1,
43
+ frequency_penalty=0,
44
+ presence_penalty=0
45
+ )
46
+
47
+ # Extract the generated text from the response
48
+ texto_respuesta = response["choices"][0]["message"]["content"]
49
+
50
+ # Ensure the response is valid JSON
51
+ try:
52
+ parsed_json = json.loads(texto_respuesta) # Attempt to parse the response
53
+ except json.JSONDecodeError:
54
+ return None, "Error: El modelo no devolvi贸 un JSON v谩lido. Por favor revise el input."
55
+
56
+ # Save the parsed JSON as a downloadable file
57
+ json_filename = "resultado.json"
58
+ with open(json_filename, "w", encoding="utf-8") as f:
59
+ json.dump(parsed_json, f, ensure_ascii=False, indent=4)
60
+
61
+ return parsed_json, json_filename
62
+
63
+ # Define Gradio app
64
+ interface = gr.Interface(
65
+ fn=generacion_llm,
66
+ inputs=gr.Textbox(label="Ingrese su texto para analizar"),
67
+ outputs=[
68
+ gr.JSON(label="Resultado JSON"), # Show the generated JSON
69
+ gr.File(label="Descargar JSON") # Allow the user to download the JSON
70
+ ],
71
+ title="Extractor de Texto a JSON",
72
+ description="Ingrese el texto para analizar y extraer informaci贸n en un formato JSON predefinido."
73
+ )
74
+
75
+ interface.launch()