INIFanalitica commited on
Commit
6086026
1 Parent(s): 3c93dc3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import textwrap
4
+ import google.generativeai as genai
5
+ import os
6
+ import PyPDF2
7
+
8
+ # Function to display formatted Markdown text
9
+ def to_markdown(text):
10
+ text = text.replace('•', ' *')
11
+ return textwrap.indent(text, '> ', predicate=lambda _: True)
12
+
13
+ # Function to generate content using Gemini API
14
+ def generate_gemini_content(prompt, model_name='gemini-pro-vision', image=None):
15
+ model = genai.GenerativeModel(model_name)
16
+ if not image:
17
+ st.warning("Por favor, agrega una imagen para usar el modelo gemini-pro-vision.")
18
+ return None
19
+
20
+ response = model.generate_content([prompt, image])
21
+ return response
22
+
23
+ # Function to extract text from PDFs in a folder
24
+ def extract_text_from_pdf(pdf_path):
25
+ text = ""
26
+ with open(pdf_path, 'rb') as file:
27
+ pdf_reader = PyPDF2.PdfFileReader(file)
28
+ num_pages = pdf_reader.numPages
29
+ for page_num in range(num_pages):
30
+ page = pdf_reader.getPage(page_num)
31
+ text += page.extractText()
32
+ return text
33
+
34
+ # Function to process PDFs in a folder
35
+ def process_pdfs_in_folder(folder_path):
36
+ pdf_texts = {}
37
+ for filename in os.listdir(folder_path):
38
+ if filename.endswith(".pdf"):
39
+ pdf_path = os.path.join(folder_path, filename)
40
+ text = extract_text_from_pdf(pdf_path)
41
+ pdf_texts[filename] = text
42
+ return pdf_texts
43
+
44
+ # Streamlit app
45
+ def main():
46
+ st.set_page_config(page_title="MAX Chatbot - INIF", page_icon="🤖")
47
+
48
+ # Configurar la API key de Gemini (reemplazar con tu clave de API de Gemini)
49
+ genai.configure(api_key='AIzaSyA4k6JoFNZsf8L1ixLMMRjEMoBPns5SHZk')
50
+
51
+ st.title("MAX Chatbot - INIF")
52
+ st.sidebar.title("Configuración de MAX Chatbot")
53
+
54
+ # Configurar la API key de INIF
55
+ inif_api_key = 'AIzaSyA4k6JoFNZsf8L1ixLMMRjEMoBPns5SHZk'
56
+ genai.configure(api_key=inif_api_key)
57
+
58
+ # Seleccionar el modelo Gemini
59
+ select_model = st.sidebar.selectbox("Selecciona el modelo", ["gemini-pro", "gemini-pro-vision"])
60
+
61
+ # Inicializar la sesión de chat
62
+ chat = genai.GenerativeModel(select_model).start_chat(history=[])
63
+
64
+ # Definir función para obtener respuesta del modelo Gemini
65
+ def get_response(messages):
66
+ response = chat.send_message(messages, stream=True)
67
+ return response
68
+
69
+ # Historial del chat
70
+ if "messages" not in st.session_state:
71
+ st.session_state["messages"] = []
72
+
73
+ messages = st.session_state["messages"]
74
+
75
+ # Obtener texto de los PDFs en la carpeta "PDF" en la misma ruta de ejecución
76
+ pdf_folder_path = "PDF"
77
+ pdf_texts = process_pdfs_in_folder(pdf_folder_path)
78
+
79
+ # Incorporar el contenido de los PDFs al contexto del INIF
80
+ inif_context = (
81
+ "I am an informative data analyst chatbot named MAX, working for the National Institute of Fraud Research and Prevention (INIF), dedicated to fraud prevention and mitigation."
82
+ " If you have questions related to fraud or prevention, feel free to ask. For inquiries about other topics, I'll redirect you to the fraud prevention context."
83
+ "\n\nContact Information for INIF:"
84
+ "\nPhone: +57 317 638 94 71"
85
+ "\nEmail: atencionalcliente@inif.com.co"
86
+ "\n\nOur Mission:"
87
+ "\nTo be the most reliable engine of knowledge, research, and information in Colombia, capable of preventing and combating fraud through synergy between our team and companies."
88
+ "\n\nOur Vision:"
89
+ "\nTo lead the construction of a more honest culture, allowing us to progress as a society."
90
+ )
91
+
92
+ for pdf_name, pdf_text in pdf_texts.items():
93
+ inif_context += f"\n\n**{pdf_name}**:\n{pdf_text}"
94
+
95
+ # Mostrar mensajes del historial
96
+ if messages:
97
+ for message in messages:
98
+ role, parts = message.values()
99
+ if role.lower() == "user":
100
+ st.markdown(f"Tú: {parts[0]}")
101
+ elif role.lower() == "model":
102
+ st.markdown(f"Assistant: {to_markdown(parts[0])}")
103
+
104
+ # Entrada del usuario
105
+ user_input = st.text_area("Tú:")
106
+
107
+ # Concatenar el contexto del INIF al input del usuario
108
+ user_input_with_context = f"{user_input}\n\n{inif_context}"
109
+
110
+ # Get optional image input if the model selected is 'gemini-pro-vision'
111
+ image_file = None
112
+ if select_model == 'gemini-pro-vision':
113
+ image_file = st.file_uploader("Sube una imagen (si aplica):", type=["jpg", "jpeg", "png"])
114
+
115
+ # Display image if provided
116
+ if image_file:
117
+ st.image(image_file, caption="Imagen subida", use_column_width=True)
118
+
119
+ # Botón para enviar mensaje o generar contenido según el modelo seleccionado
120
+ if st.button("Enviar / Generar Contenido"):
121
+ if user_input:
122
+ messages.append({"role": "user", "parts": [user_input]})
123
+ if select_model == 'gemini-pro-vision':
124
+ # Modelo Gemini Vision Pro seleccionado
125
+ if not image_file:
126
+ st.warning("Por favor, proporciona una imagen para el modelo gemini-pro-vision.")
127
+ else:
128
+ image = Image.open(image_file)
129
+ response = generate_gemini_content(user_input_with_context, model_name=select_model, image=image)
130
+ if response:
131
+ if response.candidates:
132
+ parts = response.candidates[0].content.parts
133
+ generated_text = parts[0].text if parts else "No se generó contenido."
134
+ st.markdown(f"Assistant: {to_markdown(generated_text)}")
135
+ messages.append({"role": "model", "parts": [generated_text]})
136
+ else:
137
+ st.warning("No se encontraron candidatos en la respuesta.")
138
+ else:
139
+ # Otros modelos Gemini seleccionados
140
+ response = get_response(user_input_with_context)
141
+
142
+ # Mostrar respuesta del modelo solo una vez
143
+ res_text = ""
144
+ for chunk in response:
145
+ res_text += chunk.text
146
+ st.markdown(f"Assistant: {to_markdown(res_text)}")
147
+ messages.append({"role": "model", "parts": [res_text]})
148
+
149
+ # Actualizar historial de mensajes en la sesión de Streamlit
150
+ st.session_state["messages"] = messages
151
+
152
+ if __name__ == "__main__":
153
+ main()