Spaces:
Sleeping
Sleeping
Add several functionalities and associated more logic on UI presentations.
Browse files- app.py +124 -109
- llm_call.py +102 -2
- seminar_edition_ai.py +60 -38
app.py
CHANGED
|
@@ -1,148 +1,151 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
HISTORY_ANSWER = ''
|
| 4 |
|
| 5 |
with gr.Blocks() as demo:
|
| 6 |
gr.Markdown("SermonLab AI Demo.")
|
| 7 |
with gr.Tab("Preparando mi Serm贸n"):
|
| 8 |
-
text_input = gr.Textbox(label
|
| 9 |
|
| 10 |
with gr.Accordion("Contemplando y Proclamando", open=False):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
text_output = gr.Textbox(label = "Respuesta", lines = 10)
|
| 32 |
|
| 33 |
text_button = gr.Button("Crear")
|
| 34 |
|
| 35 |
text_download = gr.DownloadButton(
|
| 36 |
-
label
|
| 37 |
-
value
|
| 38 |
-
every
|
| 39 |
-
|
| 40 |
|
| 41 |
inbtwContemplando.click(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
inbtwContemplandoOne.click(
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
inbtwContemplandoTwo.click(
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
inbtwContemplandoTree.click(
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
inbtwContemplandoFour.click(
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
|
| 71 |
inbtwContemplandoFourOne.click(
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
##---------------------------------------------------------------------
|
| 78 |
|
| 79 |
inbtwProclamando.click(
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
|
| 85 |
inbtwProclamandoOne.click(
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
| 91 |
inbtwProclamandoTwo.click(
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
|
| 97 |
inbtwProclamandoTwoTwo.click(
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
|
| 104 |
text_button.click(
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
text_download.click(
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
with gr.Tab("Obtener gu铆a de la comunidad (Preguntas)"):
|
| 115 |
with gr.Row():
|
| 116 |
#Bibliografy about components
|
| 117 |
# File (https://www.gradio.app/docs/gradio/file)
|
| 118 |
# Download Button (https://www.gradio.app/docs/gradio/downloadbutton)
|
| 119 |
with gr.Column():
|
| 120 |
-
|
| 121 |
-
|
|
|
|
| 122 |
with gr.Column():
|
| 123 |
temp_slider_question = gr.Slider(
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
)
|
| 131 |
-
text_output_question = gr.Textbox(label
|
| 132 |
text_button_question = gr.Button("Crear gu铆a de preguntas")
|
| 133 |
text_download_question = gr.DownloadButton(
|
| 134 |
-
label
|
| 135 |
-
value
|
| 136 |
-
every
|
| 137 |
-
|
| 138 |
|
| 139 |
text_button_question.click(
|
| 140 |
-
|
| 141 |
-
|
| 142 |
)
|
| 143 |
|
| 144 |
-
upload_button_question.upload(upload_file_ex, inputs=
|
| 145 |
-
|
| 146 |
|
| 147 |
with gr.Tab("Obtener gu铆a de la comunidad (Devocionario)"):
|
| 148 |
with gr.Row():
|
|
@@ -151,31 +154,43 @@ with gr.Blocks() as demo:
|
|
| 151 |
# Download Button (https://www.gradio.app/docs/gradio/downloadbutton)
|
| 152 |
|
| 153 |
with gr.Column():
|
| 154 |
-
|
| 155 |
-
|
|
|
|
| 156 |
|
| 157 |
with gr.Column():
|
| 158 |
temp_slider_question = gr.Slider(
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
)
|
| 166 |
-
text_output_devotions = gr.Textbox(label
|
| 167 |
text_button_devotion = gr.Button("Crear")
|
| 168 |
text_download_question = gr.DownloadButton(
|
| 169 |
-
label
|
| 170 |
-
value
|
| 171 |
-
every
|
| 172 |
-
|
| 173 |
|
| 174 |
text_button_devotion.click(
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
-
|
|
|
|
| 180 |
|
| 181 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llm_call import GeminiLLM
|
| 3 |
+
from seminar_edition_ai import upload_file_ex, predictContemplando, predictProclamando, predictFromInit, \
|
| 4 |
+
downloadSermonFile, fileAddresToDownload, predictQuestionBuild, predictDevotionBuild, \
|
| 5 |
+
contemplandoQuestion, proclamandoQuestion, llm, embed_model
|
| 6 |
|
| 7 |
HISTORY_ANSWER = ''
|
| 8 |
|
| 9 |
with gr.Blocks() as demo:
|
| 10 |
gr.Markdown("SermonLab AI Demo.")
|
| 11 |
with gr.Tab("Preparando mi Serm贸n"):
|
| 12 |
+
text_input = gr.Textbox(label="T贸pico del serm贸n")
|
| 13 |
|
| 14 |
with gr.Accordion("Contemplando y Proclamando", open=False):
|
| 15 |
+
checkButton = gr.Checkbox(
|
| 16 |
+
value=False,
|
| 17 |
+
label="Mantener historial"
|
| 18 |
+
)
|
| 19 |
+
with gr.Row():
|
| 20 |
+
with gr.Tab("Contemplando"):
|
| 21 |
+
inbtwContemplando = gr.Button(f"Devocionalmente: {contemplandoQuestion['DEVOCIONALMENTE']}")
|
| 22 |
+
inbtwContemplandoOne = gr.Button(f"Ex茅gesis: {contemplandoQuestion['EX脡GESIS']}")
|
| 23 |
+
inbtwContemplandoTwo = gr.Button(f"Cristo: {contemplandoQuestion['CRISTO']}")
|
| 24 |
+
inbtwContemplandoTree = gr.Button(f"Arco Redentor: {contemplandoQuestion['ARCO REDENTOR']}")
|
| 25 |
+
inbtwContemplandoFour = gr.Button(f"Evangeli贸n: {contemplandoQuestion['EVANGELION']}")
|
| 26 |
+
inbtwContemplandoFourOne = gr.Button(f"Evangeli贸n: {contemplandoQuestion['EVANGELION_TWO']}")
|
| 27 |
+
|
| 28 |
+
with gr.Tab("Proclamando"):
|
| 29 |
+
inbtwProclamando = gr.Button(f"P煤blico: {proclamandoQuestion['P脷BLICO']}")
|
| 30 |
+
inbtwProclamandoOne = gr.Button(f"Historia: {proclamandoQuestion['HISTORIA']}")
|
| 31 |
+
inbtwProclamandoTwo = gr.Button(f"Expectativas: {proclamandoQuestion['EXPECTATIVAS']}")
|
| 32 |
+
inbtwProclamandoTwoTwo = gr.Button(f"Expectativas: {proclamandoQuestion['EXPECTATIVAS_TWO']}")
|
| 33 |
+
|
| 34 |
+
text_output = gr.Textbox(label="Respuesta", lines=10)
|
|
|
|
| 35 |
|
| 36 |
text_button = gr.Button("Crear")
|
| 37 |
|
| 38 |
text_download = gr.DownloadButton(
|
| 39 |
+
label="Descargar",
|
| 40 |
+
value=fileAddresToDownload,
|
| 41 |
+
every=10
|
| 42 |
+
)
|
| 43 |
|
| 44 |
inbtwContemplando.click(
|
| 45 |
+
fn=lambda x: predictContemplando(f"DEVOCIONALMENTE"),
|
| 46 |
+
inputs=text_input,
|
| 47 |
+
outputs=text_output
|
| 48 |
+
)
|
| 49 |
|
| 50 |
inbtwContemplandoOne.click(
|
| 51 |
+
fn=lambda x: predictContemplando(f"EX脡GESIS"),
|
| 52 |
+
inputs=text_input,
|
| 53 |
+
outputs=text_output
|
| 54 |
+
)
|
| 55 |
|
| 56 |
inbtwContemplandoTwo.click(
|
| 57 |
+
fn=lambda x: predictContemplando(f"CRISTO"),
|
| 58 |
+
inputs=text_input,
|
| 59 |
+
outputs=text_output
|
| 60 |
+
)
|
| 61 |
|
| 62 |
inbtwContemplandoTree.click(
|
| 63 |
+
fn=lambda x: predictContemplando(f"ARCO REDENTOR"),
|
| 64 |
+
inputs=text_input,
|
| 65 |
+
outputs=text_output
|
| 66 |
+
)
|
| 67 |
|
| 68 |
inbtwContemplandoFour.click(
|
| 69 |
+
fn=lambda x: predictContemplando(f"EVANGELION"),
|
| 70 |
+
inputs=text_input,
|
| 71 |
+
outputs=text_output
|
| 72 |
+
)
|
| 73 |
|
| 74 |
inbtwContemplandoFourOne.click(
|
| 75 |
+
fn=lambda x: predictContemplando(f"EVANGELION_TWO"),
|
| 76 |
+
inputs=text_input,
|
| 77 |
+
outputs=text_output
|
| 78 |
+
)
|
| 79 |
|
| 80 |
##---------------------------------------------------------------------
|
| 81 |
|
| 82 |
inbtwProclamando.click(
|
| 83 |
+
fn=lambda x: predictProclamando(f"P脷BLICO"),
|
| 84 |
+
inputs=text_input,
|
| 85 |
+
outputs=text_output
|
| 86 |
+
)
|
| 87 |
|
| 88 |
inbtwProclamandoOne.click(
|
| 89 |
+
fn=lambda x: predictProclamando(f"HISTORIA"),
|
| 90 |
+
inputs=text_input,
|
| 91 |
+
outputs=text_output
|
| 92 |
+
)
|
| 93 |
|
| 94 |
inbtwProclamandoTwo.click(
|
| 95 |
+
fn=lambda x: predictProclamando(f"EXPECTATIVAS"),
|
| 96 |
+
inputs=text_input,
|
| 97 |
+
outputs=text_output
|
| 98 |
+
)
|
| 99 |
|
| 100 |
inbtwProclamandoTwoTwo.click(
|
| 101 |
+
fn=lambda x: predictProclamando(f"EXPECTATIVAS_TWO"),
|
| 102 |
+
inputs=text_input,
|
| 103 |
+
outputs=text_output
|
| 104 |
+
)
|
|
|
|
| 105 |
|
| 106 |
text_button.click(
|
| 107 |
+
fn=predictFromInit,
|
| 108 |
+
inputs=text_input,
|
| 109 |
+
outputs=text_output
|
| 110 |
+
)
|
| 111 |
|
| 112 |
text_download.click(
|
| 113 |
+
fn=downloadSermonFile,
|
| 114 |
+
inputs=text_output
|
| 115 |
+
)
|
| 116 |
with gr.Tab("Obtener gu铆a de la comunidad (Preguntas)"):
|
| 117 |
with gr.Row():
|
| 118 |
#Bibliografy about components
|
| 119 |
# File (https://www.gradio.app/docs/gradio/file)
|
| 120 |
# Download Button (https://www.gradio.app/docs/gradio/downloadbutton)
|
| 121 |
with gr.Column():
|
| 122 |
+
file_input_question = gr.File()
|
| 123 |
+
upload_button_question = gr.UploadButton("Click to Upload a File", file_types=['.pdf'],
|
| 124 |
+
file_count="multiple")
|
| 125 |
with gr.Column():
|
| 126 |
temp_slider_question = gr.Slider(
|
| 127 |
+
minimum=1,
|
| 128 |
+
maximum=10,
|
| 129 |
+
value=1,
|
| 130 |
+
step=1,
|
| 131 |
+
interactive=True,
|
| 132 |
+
label="Preguntas",
|
| 133 |
)
|
| 134 |
+
text_output_question = gr.Textbox(label="Respuesta", lines=10)
|
| 135 |
text_button_question = gr.Button("Crear gu铆a de preguntas")
|
| 136 |
text_download_question = gr.DownloadButton(
|
| 137 |
+
label="Descargar",
|
| 138 |
+
value=fileAddresToDownload,
|
| 139 |
+
every=10
|
| 140 |
+
)
|
| 141 |
|
| 142 |
text_button_question.click(
|
| 143 |
+
fn=predictQuestionBuild,
|
| 144 |
+
outputs=text_output_question
|
| 145 |
)
|
| 146 |
|
| 147 |
+
upload_button_question.upload(upload_file_ex, inputs=upload_button_question,
|
| 148 |
+
outputs=[file_input_question, text_output_question])
|
| 149 |
|
| 150 |
with gr.Tab("Obtener gu铆a de la comunidad (Devocionario)"):
|
| 151 |
with gr.Row():
|
|
|
|
| 154 |
# Download Button (https://www.gradio.app/docs/gradio/downloadbutton)
|
| 155 |
|
| 156 |
with gr.Column():
|
| 157 |
+
file_input_devotions = gr.File()
|
| 158 |
+
upload_button_devotion = gr.UploadButton("Click to Upload a File", file_types=['.pdf'],
|
| 159 |
+
file_count="multiple")
|
| 160 |
|
| 161 |
with gr.Column():
|
| 162 |
temp_slider_question = gr.Slider(
|
| 163 |
+
minimum=1,
|
| 164 |
+
maximum=10,
|
| 165 |
+
value=1,
|
| 166 |
+
step=1,
|
| 167 |
+
interactive=True,
|
| 168 |
+
label="Cantidad",
|
| 169 |
)
|
| 170 |
+
text_output_devotions = gr.Textbox(label="Respuesta", lines=10)
|
| 171 |
text_button_devotion = gr.Button("Crear")
|
| 172 |
text_download_question = gr.DownloadButton(
|
| 173 |
+
label="Descargar",
|
| 174 |
+
value=fileAddresToDownload,
|
| 175 |
+
every=10
|
| 176 |
+
)
|
| 177 |
|
| 178 |
text_button_devotion.click(
|
| 179 |
+
fn=predictDevotionBuild,
|
| 180 |
+
outputs=text_output_devotions
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
upload_button_devotion.upload(
|
| 184 |
+
upload_file_ex,
|
| 185 |
+
inputs=upload_button_devotion,
|
| 186 |
+
outputs=
|
| 187 |
+
[file_input_devotions, text_output_devotions]
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
if __name__ == "__main__":
|
| 191 |
+
llmBuilder = GeminiLLM()
|
| 192 |
|
| 193 |
+
embed_model = llmBuilder.getEmbeddingsModel()
|
| 194 |
+
llm = llmBuilder.getLLM()
|
| 195 |
|
| 196 |
+
demo.launch()
|
llm_call.py
CHANGED
|
@@ -4,7 +4,7 @@ from langchain_community.vectorstores import Chroma
|
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
from langchain.prompts import PromptTemplate
|
| 6 |
from langchain.chains import LLMChain
|
| 7 |
-
|
| 8 |
|
| 9 |
class GeminiLLM():
|
| 10 |
def __init__(self):
|
|
@@ -40,4 +40,104 @@ class GeminiLLM():
|
|
| 40 |
top_p = 1
|
| 41 |
)
|
| 42 |
|
| 43 |
-
return self.llm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
from langchain.prompts import PromptTemplate
|
| 6 |
from langchain.chains import LLMChain
|
| 7 |
+
|
| 8 |
|
| 9 |
class GeminiLLM():
|
| 10 |
def __init__(self):
|
|
|
|
| 40 |
top_p = 1
|
| 41 |
)
|
| 42 |
|
| 43 |
+
return self.llm
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
class SermonGeminiPromptTemplate():
|
| 47 |
+
# Example of {BIBLE_VERSICLE}
|
| 48 |
+
# BIBLE_VERSICLE = Juan 1:1-18
|
| 49 |
+
|
| 50 |
+
custom_prompt_template_gemini = """
|
| 51 |
+
Usted es pastor evang茅lico que est谩 preparando un serm贸n para su comunidad sobre {SERMON_TOPIC}
|
| 52 |
+
Necesito que me ayudes a encontrar los vers铆culos m谩s relevantes de la Biblia que se relacionen con este tema.
|
| 53 |
+
Por favor, proporci贸name una lista de {CANT_VERSICULOS} vers铆culos clave, citando el libro, cap铆tulo y vers铆culo.
|
| 54 |
+
Tambi茅n incluye una breve frase que resuma el significado de cada vers铆culo en relaci贸n con el tema.
|
| 55 |
+
Aseg煤rate de que los vers铆culos provengan de diferentes libros de la Biblia para tener una perspectiva amplia.
|
| 56 |
+
Formatea la salida en una lista con vi帽etas. Gracias por tu ayuda.
|
| 57 |
+
|
| 58 |
+
Context: {context}
|
| 59 |
+
|
| 60 |
+
Solo devuelve la respuesta 煤til a continuaci贸n y nada m谩s y responde siempre en espa帽ol
|
| 61 |
+
Respuesta 煤til:
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
custom_prompt_template_gemini_buildSermonStart = """
|
| 65 |
+
Usted es pastor evang茅lico que est谩 preparando un serm贸n para su comunidad.
|
| 66 |
+
Necesito que me ayudes a elaborar un serm贸n sobre los vers铆culos de la biblia
|
| 67 |
+
en {BIBLE_VERSICLE} con la estructura:
|
| 68 |
+
|
| 69 |
+
* Introducci贸n:
|
| 70 |
+
|
| 71 |
+
* Cuerpo del Serm贸n:
|
| 72 |
+
|
| 73 |
+
* Conclusi贸n:
|
| 74 |
+
|
| 75 |
+
Context: {context}
|
| 76 |
+
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
custom_prompt_template_gemini_buildSermonFronContext = """
|
| 80 |
+
Usted es pastor evang茅lico que est谩 preparando un serm贸n para su comunidad.
|
| 81 |
+
|
| 82 |
+
{SERMON_IDEA}
|
| 83 |
+
|
| 84 |
+
Context: {context}
|
| 85 |
+
|
| 86 |
+
Ahora ay煤dame a desarrollar el serm贸n siguiente estas mismas ideas
|
| 87 |
+
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
custom_prompt_template_gemini_buildSermonPrepare = """
|
| 91 |
+
Usted es pastor evang茅lico que est谩 preparando un serm贸n para su comunidad.
|
| 92 |
+
|
| 93 |
+
{SERMON_CONTEXT}
|
| 94 |
+
|
| 95 |
+
Context: {context}
|
| 96 |
+
|
| 97 |
+
Usando el texto anterior responde a la pregunta: {question}
|
| 98 |
+
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
custom_prompt_template_gemini_buildSermonQuestion = """
|
| 102 |
+
Usted es pastor evang茅lico que est谩 preparando un serm贸n para su comunidad.
|
| 103 |
+
|
| 104 |
+
{SERMON_IDEA}
|
| 105 |
+
|
| 106 |
+
Context: {context}
|
| 107 |
+
|
| 108 |
+
Elabora una gu铆a de preguntas que facilite la discusi贸n b铆blica en un grupo
|
| 109 |
+
peque帽o de estudio b铆blico de adultos a partir del serm贸n en el texto anterior
|
| 110 |
+
|
| 111 |
+
"""
|
| 112 |
+
|
| 113 |
+
custom_prompt_template_gemini_buildSermonReflections = """
|
| 114 |
+
Usted es pastor evang茅lico que est谩 preparando un serm贸n para su comunidad.
|
| 115 |
+
|
| 116 |
+
{SERMON_IDEA}
|
| 117 |
+
|
| 118 |
+
Context: {context}
|
| 119 |
+
|
| 120 |
+
Elaborar una serie de 5 reflexiones a partir del serm贸n en el texto anterior
|
| 121 |
+
|
| 122 |
+
"""
|
| 123 |
+
|
| 124 |
+
sermonPromptMenuGemini = {
|
| 125 |
+
'BUILD_INIT': custom_prompt_template_gemini,
|
| 126 |
+
'BUILD_EMPTY': custom_prompt_template_gemini_buildSermonStart,
|
| 127 |
+
'BUILD_FROM_IDEA': custom_prompt_template_gemini_buildSermonFronContext,
|
| 128 |
+
'BUILD_QUESTION': custom_prompt_template_gemini_buildSermonQuestion,
|
| 129 |
+
'BUILD_REFLECTIONS': custom_prompt_template_gemini_buildSermonReflections,
|
| 130 |
+
'BUILD_PREPARE_QUESTIONS': custom_prompt_template_gemini_buildSermonPrepare
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
def __init__(self ):
|
| 134 |
+
self.model_name = 'gemini-pro'
|
| 135 |
+
|
| 136 |
+
def getSermonPromptTemplates(self):
|
| 137 |
+
return self.sermonPromptMenuGemini
|
| 138 |
+
|
| 139 |
+
def getSermonPromptTemplate(self, sermon_id):
|
| 140 |
+
if not sermon_id in self.sermonPromptMenuGemini.values():
|
| 141 |
+
return None
|
| 142 |
+
return self.sermonPromptMenuGemini[sermon_id]
|
| 143 |
+
|
seminar_edition_ai.py
CHANGED
|
@@ -6,8 +6,15 @@ import os
|
|
| 6 |
from datetime import datetime
|
| 7 |
import pdfkit
|
| 8 |
from langchain.chains.question_answering import load_qa_chain
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
| 10 |
bookQuestion = dict()
|
|
|
|
|
|
|
| 11 |
|
| 12 |
contemplandoQuestion = {
|
| 13 |
'DEVOCIONALMENTE':'驴C贸mo estimula Dios su coraz贸n a trav茅s de Su Palabra?',
|
|
@@ -47,11 +54,18 @@ FILE_PATH_NAME = fileAddresToDownload
|
|
| 47 |
def updatePromptTemplate(promptTemplate, inputVariablesTemplate):
|
| 48 |
prompt = PromptTemplate(template = promptTemplate,
|
| 49 |
input_variables = inputVariablesTemplate)
|
| 50 |
-
chain = load_qa_chain(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return chain
|
| 52 |
def predict(query):
|
|
|
|
|
|
|
| 53 |
chain = updatePromptTemplate(
|
| 54 |
-
|
| 55 |
['question','SERMON_CONTEXT','context']
|
| 56 |
)
|
| 57 |
|
|
@@ -89,14 +103,16 @@ def predictFromInit(sermonTopic):
|
|
| 89 |
global HISTORY_ANSWER
|
| 90 |
keyStr = 'SERMON_TOPIC'
|
| 91 |
|
|
|
|
|
|
|
| 92 |
if HISTORY_ANSWER == '':
|
| 93 |
chain = updatePromptTemplate(
|
| 94 |
-
|
| 95 |
[keyStr,'CANT_VERSICULOS','context']
|
| 96 |
)
|
| 97 |
else:
|
| 98 |
chain = updatePromptTemplate(
|
| 99 |
-
|
| 100 |
['BIBLE_VERSICLE','context']
|
| 101 |
)
|
| 102 |
keyStr = 'BIBLE_VERSICLE'
|
|
@@ -134,8 +150,9 @@ def predictFromInit(sermonTopic):
|
|
| 134 |
#
|
| 135 |
####
|
| 136 |
def predictQuestionBuild(sermonTopic):
|
|
|
|
| 137 |
chain = updatePromptTemplate(
|
| 138 |
-
|
| 139 |
['SERMON_IDEA', 'context']
|
| 140 |
)
|
| 141 |
global retriever
|
|
@@ -153,8 +170,9 @@ def predictQuestionBuild(sermonTopic):
|
|
| 153 |
#
|
| 154 |
####
|
| 155 |
def predictDevotionBuild(sermonTopic):
|
|
|
|
| 156 |
chain = updatePromptTemplate(
|
| 157 |
-
|
| 158 |
['SERMON_IDEA', 'context']
|
| 159 |
)
|
| 160 |
global retriever
|
|
@@ -172,11 +190,11 @@ def predictDevotionBuild(sermonTopic):
|
|
| 172 |
|
| 173 |
# A utility function for answer generation
|
| 174 |
def askQuestion(
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
):
|
| 181 |
|
| 182 |
#Obtener los Chunks relevantes a la pregunta en el RAG
|
|
@@ -199,30 +217,22 @@ def askQuestion(
|
|
| 199 |
"input_documents": context,
|
| 200 |
"question": question
|
| 201 |
},
|
| 202 |
-
return_only_outputs=True)
|
| 203 |
)['output_text']
|
| 204 |
|
| 205 |
|
| 206 |
-
A utility function for answer generation
|
| 207 |
def askQuestionEx(
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
):
|
| 214 |
|
| 215 |
-
#Obtener los Chunks relevantes a la pregunta en el RAG
|
| 216 |
-
#print(f" Question: {question}")
|
| 217 |
-
|
| 218 |
context = _retriever.get_relevant_documents(question)
|
| 219 |
|
| 220 |
-
#print("---- Contexto ----")
|
| 221 |
-
#print(context)
|
| 222 |
-
#print("____________________GLOBAL________")
|
| 223 |
-
|
| 224 |
global HISTORY_ANSWER
|
| 225 |
-
#print (HISTORY_ANSWER)
|
| 226 |
|
| 227 |
return (
|
| 228 |
_chain({
|
|
@@ -235,11 +245,11 @@ def askQuestionEx(
|
|
| 235 |
|
| 236 |
# A utility function for answer generation
|
| 237 |
def askQuestionInit(
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
):
|
| 244 |
|
| 245 |
#Obtener los Chunks relevantes a la pregunta en el RAG
|
|
@@ -261,12 +271,6 @@ def askQuestionInit(
|
|
| 261 |
)['output_text']
|
| 262 |
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
def downloadSermonFile(answer):
|
| 271 |
|
| 272 |
if os.path.exists(FILE_PATH_NAME):
|
|
@@ -277,4 +281,22 @@ def downloadSermonFile(answer):
|
|
| 277 |
FILE_PATH_NAME
|
| 278 |
)
|
| 279 |
|
| 280 |
-
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from datetime import datetime
|
| 7 |
import pdfkit
|
| 8 |
from langchain.chains.question_answering import load_qa_chain
|
| 9 |
+
from langchain.prompts import PromptTemplate
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
import os
|
| 12 |
+
from pypdf import PdfReader
|
| 13 |
|
| 14 |
+
from llm_call import SermonGeminiPromptTemplate
|
| 15 |
bookQuestion = dict()
|
| 16 |
+
llm = None
|
| 17 |
+
embed_model = None
|
| 18 |
|
| 19 |
contemplandoQuestion = {
|
| 20 |
'DEVOCIONALMENTE':'驴C贸mo estimula Dios su coraz贸n a trav茅s de Su Palabra?',
|
|
|
|
| 54 |
def updatePromptTemplate(promptTemplate, inputVariablesTemplate):
|
| 55 |
prompt = PromptTemplate(template = promptTemplate,
|
| 56 |
input_variables = inputVariablesTemplate)
|
| 57 |
+
chain = load_qa_chain(
|
| 58 |
+
llm,
|
| 59 |
+
chain_type = "stuff",
|
| 60 |
+
prompt = prompt
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
return chain
|
| 64 |
def predict(query):
|
| 65 |
+
templates = SermonGeminiPromptTemplate()
|
| 66 |
+
|
| 67 |
chain = updatePromptTemplate(
|
| 68 |
+
templates.getSermonPromptTemplate('BUILD_PREPARE_QUESTIONS'),
|
| 69 |
['question','SERMON_CONTEXT','context']
|
| 70 |
)
|
| 71 |
|
|
|
|
| 103 |
global HISTORY_ANSWER
|
| 104 |
keyStr = 'SERMON_TOPIC'
|
| 105 |
|
| 106 |
+
templates = SermonGeminiPromptTemplate()
|
| 107 |
+
|
| 108 |
if HISTORY_ANSWER == '':
|
| 109 |
chain = updatePromptTemplate(
|
| 110 |
+
templates.getSermonPromptTemplates('BUILD_INIT'),
|
| 111 |
[keyStr,'CANT_VERSICULOS','context']
|
| 112 |
)
|
| 113 |
else:
|
| 114 |
chain = updatePromptTemplate(
|
| 115 |
+
templates.getSermonPromptTemplates('BUILD_EMPTY'),
|
| 116 |
['BIBLE_VERSICLE','context']
|
| 117 |
)
|
| 118 |
keyStr = 'BIBLE_VERSICLE'
|
|
|
|
| 150 |
#
|
| 151 |
####
|
| 152 |
def predictQuestionBuild(sermonTopic):
|
| 153 |
+
templates = SermonGeminiPromptTemplate()
|
| 154 |
chain = updatePromptTemplate(
|
| 155 |
+
templates.getSermonPromptTemplates('BUILD_QUESTION'),
|
| 156 |
['SERMON_IDEA', 'context']
|
| 157 |
)
|
| 158 |
global retriever
|
|
|
|
| 170 |
#
|
| 171 |
####
|
| 172 |
def predictDevotionBuild(sermonTopic):
|
| 173 |
+
templates = SermonGeminiPromptTemplate()
|
| 174 |
chain = updatePromptTemplate(
|
| 175 |
+
templates.getSermonPromptTemplate('BUILD_REFLECTIONS'),
|
| 176 |
['SERMON_IDEA', 'context']
|
| 177 |
)
|
| 178 |
global retriever
|
|
|
|
| 190 |
|
| 191 |
# A utility function for answer generation
|
| 192 |
def askQuestion(
|
| 193 |
+
question,
|
| 194 |
+
_chain,
|
| 195 |
+
_retriever,
|
| 196 |
+
topic = 'el amor de Dios',
|
| 197 |
+
KEY = 'SERMON_TOPIC'
|
| 198 |
):
|
| 199 |
|
| 200 |
#Obtener los Chunks relevantes a la pregunta en el RAG
|
|
|
|
| 217 |
"input_documents": context,
|
| 218 |
"question": question
|
| 219 |
},
|
| 220 |
+
return_only_outputs = True)
|
| 221 |
)['output_text']
|
| 222 |
|
| 223 |
|
| 224 |
+
#A utility function for answer generation
|
| 225 |
def askQuestionEx(
|
| 226 |
+
question,
|
| 227 |
+
_chain,
|
| 228 |
+
_retriever,
|
| 229 |
+
topic = 'el amor de Dios',
|
| 230 |
+
KEY = 'SERMON_TOPIC'
|
| 231 |
):
|
| 232 |
|
|
|
|
|
|
|
|
|
|
| 233 |
context = _retriever.get_relevant_documents(question)
|
| 234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
global HISTORY_ANSWER
|
|
|
|
| 236 |
|
| 237 |
return (
|
| 238 |
_chain({
|
|
|
|
| 245 |
|
| 246 |
# A utility function for answer generation
|
| 247 |
def askQuestionInit(
|
| 248 |
+
question,
|
| 249 |
+
_chain,
|
| 250 |
+
_retriever,
|
| 251 |
+
topic = 'el amor de Dios',
|
| 252 |
+
KEY = 'SERMON_TOPIC'
|
| 253 |
):
|
| 254 |
|
| 255 |
#Obtener los Chunks relevantes a la pregunta en el RAG
|
|
|
|
| 271 |
)['output_text']
|
| 272 |
|
| 273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
def downloadSermonFile(answer):
|
| 275 |
|
| 276 |
if os.path.exists(FILE_PATH_NAME):
|
|
|
|
| 281 |
FILE_PATH_NAME
|
| 282 |
)
|
| 283 |
|
| 284 |
+
return ""
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
def upload_file_ex(files):
|
| 288 |
+
file_paths = [file.name for file in files]
|
| 289 |
+
|
| 290 |
+
for filepath in file_paths:
|
| 291 |
+
name = Path(filepath)
|
| 292 |
+
file_content = 'Empty content'
|
| 293 |
+
|
| 294 |
+
if os.path.exists(filepath):
|
| 295 |
+
file_content = ''
|
| 296 |
+
reader = PdfReader(filepath)
|
| 297 |
+
|
| 298 |
+
for page in reader.pages:
|
| 299 |
+
file_content += page.extract_text()
|
| 300 |
+
|
| 301 |
+
HISTORY_ANSWER = file_content
|
| 302 |
+
return [file_paths, file_content]
|