Update app.py
Browse files
app.py
CHANGED
|
@@ -10,17 +10,19 @@ import uuid
|
|
| 10 |
from sentence_transformers import SentenceTransformer
|
| 11 |
import os
|
| 12 |
|
| 13 |
-
|
| 14 |
model_name = 'google/flan-t5-base'
|
| 15 |
-
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 16 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
ST_name = 'sentence-transformers/sentence-t5-base'
|
| 20 |
st_model = SentenceTransformer(ST_name)
|
|
|
|
| 21 |
|
| 22 |
client = chromadb.Client()
|
| 23 |
-
collection = client.create_collection("
|
| 24 |
|
| 25 |
|
| 26 |
def get_context(query_text):
|
|
@@ -33,7 +35,7 @@ def get_context(query_text):
|
|
| 33 |
|
| 34 |
def local_query(query, context):
|
| 35 |
t5query = """Using the available context, please answer the question.
|
| 36 |
-
If you
|
| 37 |
Context: {}
|
| 38 |
Question: {}
|
| 39 |
""".format(context, query)
|
|
@@ -50,9 +52,20 @@ def local_query(query, context):
|
|
| 50 |
|
| 51 |
def run_query(btn, history, query):
|
| 52 |
|
| 53 |
-
context = get_context(query)
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
return history, ""
|
| 57 |
|
| 58 |
|
|
@@ -94,10 +107,13 @@ def upload_pdf(file):
|
|
| 94 |
return f"An error occurred: {e}"
|
| 95 |
|
| 96 |
|
|
|
|
|
|
|
|
|
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
|
| 99 |
btn = gr.UploadButton("Upload a PDF", file_types=[".pdf"])
|
| 100 |
-
output = gr.Textbox(label="Output Box"
|
| 101 |
chatbot = gr.Chatbot(height=240)
|
| 102 |
|
| 103 |
with gr.Row():
|
|
@@ -107,30 +123,14 @@ with gr.Blocks() as demo:
|
|
| 107 |
placeholder="Enter a question",
|
| 108 |
)
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
# with gr.Blocks() as demo:
|
| 113 |
-
|
| 114 |
-
# btn = gr.UploadButton("Upload a PDF", file_types=[".pdf"])
|
| 115 |
-
# output = gr.Textbox(label="Output Box", style={"height": "100px", "margin-top": "20px"})
|
| 116 |
-
# chatbot = gr.Chatbot(height=240, placeholder="Ask me anything...", style={"margin-top": "20px"})
|
| 117 |
-
|
| 118 |
-
# with gr.Row(style={"margin-top": "20px"}):
|
| 119 |
-
# with gr.Column(scale=0.70):
|
| 120 |
-
# # Styled Textbox
|
| 121 |
-
# txt = gr.Textbox(
|
| 122 |
-
# show_label=False,
|
| 123 |
-
# placeholder="Enter a question",
|
| 124 |
-
# style={"width": "100%", "height": "100px", "margin-bottom": "10px"}
|
| 125 |
-
# )
|
| 126 |
-
|
| 127 |
|
| 128 |
-
#
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
| 131 |
|
| 132 |
|
| 133 |
gr.close_all()
|
| 134 |
-
|
| 135 |
-
demo.queue().launch()
|
| 136 |
-
|
|
|
|
| 10 |
from sentence_transformers import SentenceTransformer
|
| 11 |
import os
|
| 12 |
|
| 13 |
+
|
| 14 |
model_name = 'google/flan-t5-base'
|
| 15 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name, device_map='auto', offload_folder="offload")
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 17 |
+
print('flan read')
|
| 18 |
|
| 19 |
|
| 20 |
ST_name = 'sentence-transformers/sentence-t5-base'
|
| 21 |
st_model = SentenceTransformer(ST_name)
|
| 22 |
+
print('sentence read')
|
| 23 |
|
| 24 |
client = chromadb.Client()
|
| 25 |
+
collection = client.create_collection("test_db")
|
| 26 |
|
| 27 |
|
| 28 |
def get_context(query_text):
|
|
|
|
| 35 |
|
| 36 |
def local_query(query, context):
|
| 37 |
t5query = """Using the available context, please answer the question.
|
| 38 |
+
If you aren't sure please say i don't know.
|
| 39 |
Context: {}
|
| 40 |
Question: {}
|
| 41 |
""".format(context, query)
|
|
|
|
| 52 |
|
| 53 |
def run_query(btn, history, query):
|
| 54 |
|
| 55 |
+
context = get_context(query)
|
| 56 |
+
|
| 57 |
+
print('calling local query')
|
| 58 |
+
result = local_query(query, context)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
print('printing result after call back')
|
| 62 |
+
print(result)
|
| 63 |
+
|
| 64 |
+
history.append((query, str(result[0])))
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
print('printing history')
|
| 68 |
+
print(history)
|
| 69 |
return history, ""
|
| 70 |
|
| 71 |
|
|
|
|
| 107 |
return f"An error occurred: {e}"
|
| 108 |
|
| 109 |
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
with gr.Blocks() as demo:
|
| 114 |
|
| 115 |
btn = gr.UploadButton("Upload a PDF", file_types=[".pdf"])
|
| 116 |
+
output = gr.Textbox(label="Output Box")
|
| 117 |
chatbot = gr.Chatbot(height=240)
|
| 118 |
|
| 119 |
with gr.Row():
|
|
|
|
| 123 |
placeholder="Enter a question",
|
| 124 |
)
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# Event handler for uploading a PDF
|
| 128 |
+
btn.upload(fn=upload_pdf, inputs=[btn], outputs=[output])
|
| 129 |
+
txt.submit(run_query, [btn, chatbot, txt], [chatbot, txt])
|
| 130 |
+
#.then(
|
| 131 |
+
# generate_response, inputs =[chatbot,],outputs = chatbot,)
|
| 132 |
|
| 133 |
|
| 134 |
gr.close_all()
|
| 135 |
+
# demo.launch(share=True)
|
| 136 |
+
demo.queue().launch()
|
|
|