Update app.py
Browse files
app.py
CHANGED
@@ -12,30 +12,12 @@ import base64
|
|
12 |
# Load environment variables
|
13 |
load_dotenv()
|
14 |
|
15 |
-
llm_models =
|
16 |
-
"
|
17 |
-
"
|
18 |
-
"
|
19 |
-
"
|
20 |
-
|
21 |
-
# "NousResearch/Yarn-Mistral-7b-64k", ## 14GB>10GB
|
22 |
-
# "impira/layoutlm-document-qa", ## ERR
|
23 |
-
# "Qwen/Qwen1.5-7B", ## 15GB
|
24 |
-
# "Qwen/Qwen2.5-3B", ## high response time
|
25 |
-
# "google/gemma-2-2b-jpn-it", ## high response time
|
26 |
-
# "impira/layoutlm-invoices", ## bad req
|
27 |
-
# "google/pix2struct-docvqa-large", ## bad req
|
28 |
-
# "google/gemma-7b-it", ## 17GB > 10GB
|
29 |
-
# "google/gemma-2b-it", ## high response time
|
30 |
-
# "HuggingFaceH4/zephyr-7b-beta", ## high response time
|
31 |
-
# "HuggingFaceH4/zephyr-7b-gemma-v0.1", ## bad req
|
32 |
-
# "microsoft/phi-2", ## high response time
|
33 |
-
# "TinyLlama/TinyLlama-1.1B-Chat-v1.0", ## high response time
|
34 |
-
# "mosaicml/mpt-7b-instruct", ## 13GB>10GB
|
35 |
-
# "google/flan-t5-xxl" ## high respons time
|
36 |
-
# "NousResearch/Yarn-Mistral-7b-128k", ## 14GB>10GB
|
37 |
-
# "Qwen/Qwen2.5-7B-Instruct", ## 15GB>10GB
|
38 |
-
]
|
39 |
|
40 |
embed_models = [
|
41 |
"BAAI/bge-small-en-v1.5", # 33.4M
|
@@ -45,49 +27,48 @@ embed_models = [
|
|
45 |
]
|
46 |
|
47 |
# Global variable for selected model
|
48 |
-
selected_llm_model_name = llm_models[0] # Default to the first model in the
|
49 |
-
|
50 |
vector_index = None
|
51 |
|
52 |
# Initialize the parser
|
53 |
parser = LlamaParse(api_key=os.getenv("LLAMA_INDEX_API"), result_type='markdown')
|
54 |
-
|
55 |
file_extractor = {
|
56 |
-
'.pdf': parser,
|
57 |
-
'.docx': parser,
|
58 |
-
'.
|
59 |
-
'.
|
60 |
-
'.
|
61 |
-
'.
|
62 |
-
'.
|
63 |
-
'.
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
}
|
81 |
|
82 |
|
83 |
# File processing function
|
84 |
def load_files(file_path: str, embed_model_name: str):
|
85 |
try:
|
86 |
-
global vector_index
|
87 |
document = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()
|
88 |
embed_model = HuggingFaceEmbedding(model_name=embed_model_name)
|
89 |
vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
|
90 |
-
|
91 |
filename = os.path.basename(file_path)
|
92 |
return f"Ready to give response on {filename}"
|
93 |
except Exception as e:
|
@@ -97,81 +78,81 @@ def load_files(file_path: str, embed_model_name: str):
|
|
97 |
# Function to handle the selected model from dropdown
|
98 |
def set_llm_model(selected_model):
|
99 |
global selected_llm_model_name
|
100 |
-
selected_llm_model_name =
|
101 |
-
# print(f"Model selected: {selected_model_name}")
|
102 |
-
# return f"Model set to: {selected_model_name}"
|
103 |
|
104 |
|
105 |
-
|
|
|
|
|
106 |
def respond(message, history):
|
107 |
try:
|
108 |
-
|
109 |
llm = HuggingFaceInferenceAPI(
|
110 |
model_name=selected_llm_model_name,
|
111 |
-
contextWindow=8192,
|
112 |
-
maxTokens=1024,
|
113 |
-
temperature=0.3,
|
114 |
-
topP=0.9,
|
115 |
-
frequencyPenalty=0.5,
|
116 |
-
presencePenalty=0.5,
|
117 |
token=os.getenv("TOKEN")
|
118 |
)
|
119 |
|
120 |
-
|
121 |
query_engine = vector_index.as_query_engine(llm=llm)
|
122 |
bot_message = query_engine.query(message)
|
|
|
|
|
123 |
|
124 |
-
print(f"\n{datetime.now()}:{selected_llm_model_name}:: {message} --> {str(bot_message)}\n")
|
125 |
-
return f"{selected_llm_model_name}:\n{str(bot_message)}"
|
126 |
except Exception as e:
|
127 |
if str(e) == "'NoneType' object has no attribute 'as_query_engine'":
|
128 |
return "Please upload a file."
|
129 |
return f"An error occurred: {e}"
|
130 |
|
131 |
-
def encode_image(image_path):
|
132 |
-
with open(image_path, "rb") as image_file:
|
133 |
-
return base64.b64encode(image_file.read()).decode('utf-8')
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
139 |
|
140 |
# UI Setup
|
141 |
-
with gr.Blocks(theme=
|
142 |
-
gr.Markdown("
|
143 |
with gr.Tabs():
|
144 |
-
with gr.TabItem("
|
145 |
gr.Markdown(md.description)
|
146 |
|
147 |
-
with gr.TabItem("
|
148 |
-
with gr.Accordion("
|
149 |
guid = gr.Markdown(md.guide)
|
150 |
with gr.Row():
|
151 |
with gr.Column(scale=1):
|
152 |
-
file_input = gr.File(file_count="single", type='filepath', label="
|
153 |
-
|
154 |
-
|
155 |
with gr.Row():
|
156 |
btn = gr.Button("Submit", variant='primary')
|
157 |
clear = gr.ClearButton()
|
158 |
output = gr.Text(label='Vector Index')
|
159 |
-
llm_model_dropdown = gr.Dropdown(llm_models, label="
|
160 |
with gr.Column(scale=3):
|
161 |
gr.ChatInterface(
|
162 |
fn=respond,
|
163 |
chatbot=gr.Chatbot(height=500),
|
164 |
-
theme
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
)
|
169 |
-
|
170 |
-
|
171 |
llm_model_dropdown.change(fn=set_llm_model, inputs=llm_model_dropdown)
|
172 |
btn.click(fn=load_files, inputs=[file_input, embed_model_dropdown], outputs=output)
|
173 |
clear.click(lambda: [None] * 3, outputs=[file_input, embed_model_dropdown, output])
|
174 |
|
175 |
-
|
176 |
if __name__ == "__main__":
|
177 |
demo.launch()
|
|
|
12 |
# Load environment variables
|
13 |
load_dotenv()
|
14 |
|
15 |
+
llm_models = {
|
16 |
+
"tiiuae/falcon-7b-instruct": "HundAI-7B-S",
|
17 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1": "Mixtral-8x7B",
|
18 |
+
"meta-llama/Meta-Llama-3-8B-Instruct": "Meta-Llama-8B",
|
19 |
+
"mistralai/Mistral-7B-Instruct-v0.2": "Mistral-7B",
|
20 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
embed_models = [
|
23 |
"BAAI/bge-small-en-v1.5", # 33.4M
|
|
|
27 |
]
|
28 |
|
29 |
# Global variable for selected model
|
30 |
+
selected_llm_model_name = list(llm_models.keys())[0] # Default to the first model in the dictionary
|
31 |
+
|
32 |
vector_index = None
|
33 |
|
34 |
# Initialize the parser
|
35 |
parser = LlamaParse(api_key=os.getenv("LLAMA_INDEX_API"), result_type='markdown')
|
36 |
+
|
37 |
file_extractor = {
|
38 |
+
'.pdf': parser,
|
39 |
+
'.docx': parser,
|
40 |
+
'.txt': parser,
|
41 |
+
'.csv': parser,
|
42 |
+
'.xlsx': parser,
|
43 |
+
'.pptx': parser,
|
44 |
+
'.html': parser,
|
45 |
+
'.jpg': parser,
|
46 |
+
'.jpeg': parser,
|
47 |
+
'.png': parser,
|
48 |
+
'.webp': parser,
|
49 |
+
'.svg': parser,
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
}
|
63 |
|
64 |
|
65 |
# File processing function
|
66 |
def load_files(file_path: str, embed_model_name: str):
|
67 |
try:
|
|
|
68 |
document = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()
|
69 |
embed_model = HuggingFaceEmbedding(model_name=embed_model_name)
|
70 |
vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
|
71 |
+
|
72 |
filename = os.path.basename(file_path)
|
73 |
return f"Ready to give response on {filename}"
|
74 |
except Exception as e:
|
|
|
78 |
# Function to handle the selected model from dropdown
|
79 |
def set_llm_model(selected_model):
|
80 |
global selected_llm_model_name
|
81 |
+
selected_llm_model_name = next(key for key, value in llm_models.items() if value == selected_model)
|
|
|
|
|
82 |
|
83 |
|
84 |
+
|
85 |
+
# Respond function
|
86 |
+
|
87 |
def respond(message, history):
|
88 |
try:
|
89 |
+
|
90 |
llm = HuggingFaceInferenceAPI(
|
91 |
model_name=selected_llm_model_name,
|
92 |
+
contextWindow=8192,
|
93 |
+
maxTokens=1024,
|
94 |
+
temperature=0.3,
|
95 |
+
topP=0.9,
|
96 |
+
frequencyPenalty=0.5,
|
97 |
+
presencePenalty=0.5,
|
98 |
token=os.getenv("TOKEN")
|
99 |
)
|
100 |
|
101 |
+
|
102 |
query_engine = vector_index.as_query_engine(llm=llm)
|
103 |
bot_message = query_engine.query(message)
|
104 |
+
return f"{llm_models[selected_llm_model_name]}:\n{str(bot_message)}"
|
105 |
+
|
106 |
|
|
|
|
|
107 |
except Exception as e:
|
108 |
if str(e) == "'NoneType' object has no attribute 'as_query_engine'":
|
109 |
return "Please upload a file."
|
110 |
return f"An error occurred: {e}"
|
111 |
|
|
|
|
|
|
|
112 |
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
|
121 |
# UI Setup
|
122 |
+
with gr.Blocks(theme='Hev832/Applio', css='footer {visibility: hidden}') as demo:
|
123 |
+
gr.Markdown("")
|
124 |
with gr.Tabs():
|
125 |
+
with gr.TabItem("Introduction"):
|
126 |
gr.Markdown(md.description)
|
127 |
|
128 |
+
with gr.TabItem("Chatbot"):
|
129 |
+
with gr.Accordion("IMPORTANT: READ ME FIRST", open=False):
|
130 |
guid = gr.Markdown(md.guide)
|
131 |
with gr.Row():
|
132 |
with gr.Column(scale=1):
|
133 |
+
file_input = gr.File(file_count="single", type='filepath', label="Upload document")
|
134 |
+
embed_model_dropdown = gr.Dropdown(embed_models, label="Select Embedding", interactive=True)
|
135 |
+
|
136 |
with gr.Row():
|
137 |
btn = gr.Button("Submit", variant='primary')
|
138 |
clear = gr.ClearButton()
|
139 |
output = gr.Text(label='Vector Index')
|
140 |
+
llm_model_dropdown = gr.Dropdown(list(llm_models.values()), label="Select LLM", interactive=True)
|
141 |
with gr.Column(scale=3):
|
142 |
gr.ChatInterface(
|
143 |
fn=respond,
|
144 |
chatbot=gr.Chatbot(height=500),
|
145 |
+
theme="soft",
|
146 |
+
textbox=gr.Textbox(placeholder="Ask me any questions on the uploaded document!", container=False)
|
147 |
+
|
148 |
+
|
149 |
)
|
150 |
+
|
151 |
+
|
152 |
llm_model_dropdown.change(fn=set_llm_model, inputs=llm_model_dropdown)
|
153 |
btn.click(fn=load_files, inputs=[file_input, embed_model_dropdown], outputs=output)
|
154 |
clear.click(lambda: [None] * 3, outputs=[file_input, embed_model_dropdown, output])
|
155 |
|
156 |
+
|
157 |
if __name__ == "__main__":
|
158 |
demo.launch()
|