Spaces:
Paused
Paused
functional
Browse files
app.py
CHANGED
@@ -1,3 +1,5 @@
|
|
|
|
|
|
1 |
import torch
|
2 |
from transformers import BitsAndBytesConfig
|
3 |
from llama_index.llms.huggingface import HuggingFaceLLM
|
@@ -6,7 +8,7 @@ from llama_index.core import SimpleDirectoryReader
|
|
6 |
from llama_index.core import VectorStoreIndex, SummaryIndex
|
7 |
from llama_index.core.prompts import PromptTemplate
|
8 |
from llama_index.core import Settings
|
9 |
-
|
10 |
|
11 |
import gradio as gr
|
12 |
|
@@ -63,6 +65,9 @@ def load_RAG_pipeline():
|
|
63 |
# raw data
|
64 |
documents = SimpleDirectoryReader("assets/txts").load_data()
|
65 |
vector_index = VectorStoreIndex.from_documents(documents)
|
|
|
|
|
|
|
66 |
# summary_index = SummaryIndex.from_documents(documents)
|
67 |
query_engine = vector_index.as_query_engine(response_mode="compact", similarity_top_k=3)
|
68 |
return query_engine
|
@@ -78,23 +83,31 @@ def get_answer(question, temperature, nucleus_sampling, max_tokens):
|
|
78 |
# For example, you could use a machine learning model for RAG.
|
79 |
# answer = "This is a placeholder answer."
|
80 |
# https://docs.llamaindex.ai/en/stable/module_guides/supporting_modules/settings/#setting-local-configurations
|
81 |
-
|
|
|
82 |
|
83 |
|
84 |
-
def get_answer_page(
|
85 |
# Implement logic to retrieve the page number or an image of the page with the answer.
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
88 |
|
89 |
|
90 |
# Create the gr.Interface function
|
91 |
def ask_my_thesis(question, temperature, nucleus_sampling, max_tokens):
|
92 |
answer = get_answer(question, temperature, nucleus_sampling, max_tokens)
|
93 |
-
answer_page = get_answer_page(
|
94 |
-
return answer, answer_page
|
95 |
|
96 |
|
97 |
# Set up the interface options based on the design in the image.
|
|
|
|
|
|
|
|
|
98 |
iface = gr.Interface(
|
99 |
fn=ask_my_thesis,
|
100 |
inputs=[
|
@@ -103,12 +116,17 @@ iface = gr.Interface(
|
|
103 |
gr.Slider(0, 1, value=0.9, label="Nucleus Sampling"),
|
104 |
gr.Slider(1, 500, value=100, label="Max Generated Number of Tokens"),
|
105 |
],
|
106 |
-
outputs=[gr.Textbox(label="Answer"), gr.
|
107 |
-
title="Ask my thesis",
|
108 |
-
description="Chat with the manuscript: ask questions and receive answers with references.
|
|
|
|
|
|
|
109 |
allow_flagging="never",
|
110 |
)
|
|
|
111 |
|
|
|
112 |
# Start the application.
|
113 |
if __name__ == "__main__":
|
114 |
iface.launch()
|
|
|
1 |
+
# TODO: return all pages used to form answer
|
2 |
+
|
3 |
import torch
|
4 |
from transformers import BitsAndBytesConfig
|
5 |
from llama_index.llms.huggingface import HuggingFaceLLM
|
|
|
8 |
from llama_index.core import VectorStoreIndex, SummaryIndex
|
9 |
from llama_index.core.prompts import PromptTemplate
|
10 |
from llama_index.core import Settings
|
11 |
+
from PIL import Image
|
12 |
|
13 |
import gradio as gr
|
14 |
|
|
|
65 |
# raw data
|
66 |
documents = SimpleDirectoryReader("assets/txts").load_data()
|
67 |
vector_index = VectorStoreIndex.from_documents(documents)
|
68 |
+
# vector_index.persist(persist_dir="vectors")
|
69 |
+
# https://docs.llamaindex.ai/en/v0.10.17/understanding/storing/storing.html
|
70 |
+
|
71 |
# summary_index = SummaryIndex.from_documents(documents)
|
72 |
query_engine = vector_index.as_query_engine(response_mode="compact", similarity_top_k=3)
|
73 |
return query_engine
|
|
|
83 |
# For example, you could use a machine learning model for RAG.
|
84 |
# answer = "This is a placeholder answer."
|
85 |
# https://docs.llamaindex.ai/en/stable/module_guides/supporting_modules/settings/#setting-local-configurations
|
86 |
+
response = query_engine.query(question)
|
87 |
+
return response
|
88 |
|
89 |
|
90 |
+
def get_answer_page(response):
|
91 |
# Implement logic to retrieve the page number or an image of the page with the answer.
|
92 |
+
# best image
|
93 |
+
best_match = response.source_nodes[0].metadata["file_path"]
|
94 |
+
answer_page = float(int(best_match[-8:-4]))
|
95 |
+
image = Image.open(best_match.replace("txt", "png"))
|
96 |
+
return image, answer_page
|
97 |
|
98 |
|
99 |
# Create the gr.Interface function
|
100 |
def ask_my_thesis(question, temperature, nucleus_sampling, max_tokens):
|
101 |
answer = get_answer(question, temperature, nucleus_sampling, max_tokens)
|
102 |
+
image, answer_page = get_answer_page(answer)
|
103 |
+
return answer, image, answer_page
|
104 |
|
105 |
|
106 |
# Set up the interface options based on the design in the image.
|
107 |
+
output_image = gr.Image(label="Answer Page")
|
108 |
+
|
109 |
+
# examples
|
110 |
+
|
111 |
iface = gr.Interface(
|
112 |
fn=ask_my_thesis,
|
113 |
inputs=[
|
|
|
116 |
gr.Slider(0, 1, value=0.9, label="Nucleus Sampling"),
|
117 |
gr.Slider(1, 500, value=100, label="Max Generated Number of Tokens"),
|
118 |
],
|
119 |
+
outputs=[gr.Textbox(label="Answer"), output_image, gr.Label()],
|
120 |
+
title="Ask my thesis: Intelligent Automation for AI-Driven Document Understanding",
|
121 |
+
description=r"""Chat with the thesis manuscript: ask questions and receive answers with multimodal references (WIP).
|
122 |
+
|
123 |
+
Spoiler: RAG application with LLM and embedding vector store can be quite slow on a 290 page document ;D
|
124 |
+
""",
|
125 |
allow_flagging="never",
|
126 |
)
|
127 |
+
# https://github.com/gradio-app/gradio/issues/4309
|
128 |
|
129 |
+
# https://discuss.huggingface.co/t/add-background-image/16381/4 background image
|
130 |
# Start the application.
|
131 |
if __name__ == "__main__":
|
132 |
iface.launch()
|