Update app.py
Browse files
app.py
CHANGED
@@ -1,148 +1,63 @@
|
|
|
|
|
|
|
|
1 |
from datetime import datetime
|
2 |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
3 |
-
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
4 |
from llama_parse import LlamaParse
|
|
|
5 |
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
6 |
-
import os
|
7 |
-
from dotenv import load_dotenv
|
8 |
-
import gradio as gr
|
9 |
-
import markdowm as md
|
10 |
-
import base64
|
11 |
-
|
12 |
-
# Load environment variables
|
13 |
-
load_dotenv()
|
14 |
-
|
15 |
-
llm_models = [
|
16 |
-
"tiiuae/falcon-7b-instruct",
|
17 |
-
]
|
18 |
-
|
19 |
-
embed_models = [
|
20 |
-
"BAAI/bge-small-en-v1.5", # 33.4M
|
21 |
-
]
|
22 |
-
|
23 |
-
# Global variable for selected model
|
24 |
-
selected_llm_model_name = llm_models[0] # Default to the first model in the list
|
25 |
-
selected_embed_model_name = embed_models[0] # Default to the first model in the list
|
26 |
-
vector_index = None
|
27 |
-
|
28 |
-
# Initialize the parser
|
29 |
-
parser = LlamaParse(api_key="llx-zKtsC5UBLs8DOApOsLluXMBdQhC75ea0Vs80SmPSjsmDzuhh", result_type='markdown')
|
30 |
-
# Define file extractor with various common extensions
|
31 |
-
file_extractor = {
|
32 |
-
'.pdf': parser, # PDF documents
|
33 |
-
'.docx': parser, # Microsoft Word documents
|
34 |
-
'.doc': parser, # Older Microsoft Word documents
|
35 |
-
'.txt': parser, # Plain text files
|
36 |
-
'.csv': parser, # Comma-separated values files
|
37 |
-
'.xlsx': parser, # Microsoft Excel files (requires additional processing for tables)
|
38 |
-
'.pptx': parser, # Microsoft PowerPoint files (for slides)
|
39 |
-
'.html': parser, # HTML files (web pages)
|
40 |
-
|
41 |
-
|
42 |
-
# Image files for OCR processing
|
43 |
-
'.jpg': parser, # JPEG images
|
44 |
-
'.jpeg': parser, # JPEG images
|
45 |
-
'.png': parser, # PNG images
|
46 |
-
|
47 |
-
|
48 |
-
# Scanned documents in image formats
|
49 |
-
'.webp': parser, # WebP images
|
50 |
-
'.svg': parser, # SVG files (vector format, may contain embedded text)
|
51 |
-
}
|
52 |
-
|
53 |
-
|
54 |
-
# File processing function
|
55 |
-
def load_files(file_path: str, embed_model_name: str):
|
56 |
-
try:
|
57 |
-
global vector_index
|
58 |
-
document = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()
|
59 |
-
embed_model = HuggingFaceEmbedding(model_name=embed_model_name)
|
60 |
-
vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
|
61 |
-
print(f"Parsing done for {file_path}")
|
62 |
-
filename = os.path.basename(file_path)
|
63 |
-
return f"Ready to give response on {filename}"
|
64 |
-
except Exception as e:
|
65 |
-
return f"An error occurred: {e}"
|
66 |
-
|
67 |
-
|
68 |
-
# Function to handle the selected model from dropdown
|
69 |
-
def set_llm_model(selected_model):
|
70 |
-
global selected_llm_model_name
|
71 |
-
selected_llm_model_name = selected_model # Update the global variable
|
72 |
-
# print(f"Model selected: {selected_model_name}")
|
73 |
-
# return f"Model set to: {selected_model_name}"
|
74 |
-
|
75 |
-
|
76 |
-
# Respond function that uses the globally set selected model
|
77 |
-
def respond(message, history):
|
78 |
-
try:
|
79 |
-
# Initialize the LLM with the selected model
|
80 |
-
llm = HuggingFaceInferenceAPI(
|
81 |
-
model_name=selected_llm_model_name,
|
82 |
-
contextWindow=8192, # Context window size (typically max length of the model)
|
83 |
-
maxTokens=1024, # Tokens per response generation (512-1024 works well for detailed answers)
|
84 |
-
temperature=0.3, # Lower temperature for more focused answers (0.2-0.4 for factual info)
|
85 |
-
topP=0.9, # Top-p sampling to control diversity while retaining quality
|
86 |
-
frequencyPenalty=0.5, # Slight penalty to avoid repetition
|
87 |
-
presencePenalty=0.5, # Encourages exploration without digressing too much
|
88 |
-
token=os.getenv("TOKEN")
|
89 |
-
)
|
90 |
-
|
91 |
-
# Set up the query engine with the selected LLM
|
92 |
-
query_engine = vector_index.as_query_engine(llm=llm)
|
93 |
-
bot_message = query_engine.query(message)
|
94 |
-
|
95 |
-
print(f"\n{datetime.now()}:{selected_llm_model_name}:: {message} --> {str(bot_message)}\n")
|
96 |
-
return f"{selected_llm_model_name}:\n{str(bot_message)}"
|
97 |
-
except Exception as e:
|
98 |
-
if str(e) == "'NoneType' object has no attribute 'as_query_engine'":
|
99 |
-
return "Please upload a file."
|
100 |
-
return f"An error occurred: {e}"
|
101 |
-
|
102 |
-
def encode_image(image_path):
|
103 |
-
with open(image_path, "rb") as image_file:
|
104 |
-
return base64.b64encode(image_file.read()).decode('utf-8')
|
105 |
-
|
106 |
-
# Encode the images
|
107 |
-
github_logo_encoded = encode_image("Images/github-logo.png")
|
108 |
-
linkedin_logo_encoded = encode_image("Images/linkedin-logo.png")
|
109 |
-
website_logo_encoded = encode_image("Images/ai-logo.png")
|
110 |
-
|
111 |
-
# UI Setup
|
112 |
-
with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]), css='footer {visibility: hidden}') as demo:
|
113 |
-
gr.Markdown("# DocBot📄🤖")
|
114 |
-
with gr.Tabs():
|
115 |
-
with gr.TabItem("Intro"):
|
116 |
-
gr.Markdown(md.description)
|
117 |
-
|
118 |
-
with gr.TabItem("DocBot"):
|
119 |
-
with gr.Accordion("=== IMPORTANT: READ ME FIRST ===", open=False):
|
120 |
-
guid = gr.Markdown(md.guide)
|
121 |
-
with gr.Row():
|
122 |
-
with gr.Column(scale=1):
|
123 |
-
file_input = gr.File(file_count="single", type='filepath', label="Step-1: Upload document")
|
124 |
-
# gr.Markdown("Dont know what to select check out in Intro tab")
|
125 |
-
embed_model_dropdown = gr.Dropdown(embed_models, label="Step-2: Select Embedding", interactive=True)
|
126 |
-
with gr.Row():
|
127 |
-
btn = gr.Button("Submit", variant='primary')
|
128 |
-
clear = gr.ClearButton()
|
129 |
-
output = gr.Text(label='Vector Index')
|
130 |
-
llm_model_dropdown = gr.Dropdown(llm_models, label="Step-3: Select LLM", interactive=True)
|
131 |
-
with gr.Column(scale=3):
|
132 |
-
gr.ChatInterface(
|
133 |
-
fn=respond,
|
134 |
-
chatbot=gr.Chatbot(height=500),
|
135 |
-
theme = "soft",
|
136 |
-
show_progress='full',
|
137 |
-
# cache_mode='lazy',
|
138 |
-
textbox=gr.Textbox(placeholder="Step-4: Ask me questions on the uploaded document!", container=False)
|
139 |
-
)
|
140 |
-
gr.HTML(md.footer.format(github_logo_encoded, linkedin_logo_encoded, website_logo_encoded))
|
141 |
-
# Set up Gradio interactions
|
142 |
-
llm_model_dropdown.change(fn=set_llm_model, inputs=llm_model_dropdown)
|
143 |
-
btn.click(fn=load_files, inputs=[file_input, embed_model_dropdown], outputs=output)
|
144 |
-
clear.click(lambda: [None] * 3, outputs=[file_input, embed_model_dropdown, output])
|
145 |
|
146 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
if __name__ == "__main__":
|
148 |
-
demo.launch(
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import httpx
|
3 |
+
import time
|
4 |
from datetime import datetime
|
5 |
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
|
|
6 |
from llama_parse import LlamaParse
|
7 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
8 |
from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# LLM ve Parser Başlatma
|
11 |
+
llm = HuggingFaceInferenceAPI(model_name="meta-llama/Llama-3.2-1B")
|
12 |
+
parser = LlamaParse(api_key='llx-zKtsC5UBLs8DOApOsLluXMBdQhC75ea0Vs80SmPSjsmDzuhh', result_type='markdown')
|
13 |
+
|
14 |
+
# PDF dosyasını yükleyip indexleme işlemi
|
15 |
+
# Bu kısımda pdf zaten sistemde yüklü olduğundan 'data/' dizininde olduğundan emin olun
|
16 |
+
file_extractor = {'.pdf': parser}
|
17 |
+
documents = SimpleDirectoryReader('data/', file_extractor=file_extractor).load_data()
|
18 |
+
|
19 |
+
# Embedding Modeli ve Query Engine Başlatma
|
20 |
+
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
21 |
+
vector_index = VectorStoreIndex.from_documents(documents, embed_model=embed_model)
|
22 |
+
query_engine = vector_index.as_query_engine(llm=llm)
|
23 |
+
|
24 |
+
# Sorgu işlemi için retry mekanizması
|
25 |
+
def query_with_retry(query, max_retries=3, wait_time=5):
|
26 |
+
for attempt in range(max_retries):
|
27 |
+
try:
|
28 |
+
start_time = datetime.now()
|
29 |
+
response = query_engine.query(query)
|
30 |
+
end_time = datetime.now()
|
31 |
+
duration = (end_time - start_time).total_seconds()
|
32 |
+
print(f"Query completed in {duration:.2f} seconds.\n {response}")
|
33 |
+
return response
|
34 |
+
except httpx.ReadTimeout:
|
35 |
+
if attempt < max_retries - 1:
|
36 |
+
print(f"Timeout occurred. Retrying in {wait_time} seconds...")
|
37 |
+
time.sleep(wait_time)
|
38 |
+
else:
|
39 |
+
raise
|
40 |
+
except Exception as e:
|
41 |
+
print(f"An error occurred: {e}")
|
42 |
+
break
|
43 |
+
|
44 |
+
# Gradio arayüzü
|
45 |
+
def respond_to_pdf(query):
|
46 |
+
return query_with_retry(query)
|
47 |
+
|
48 |
+
# Basit Gradio Arayüzü
|
49 |
+
with gr.Blocks() as demo:
|
50 |
+
gr.Markdown("# PDF DocBot")
|
51 |
+
|
52 |
+
# Kullanıcıların PDF'e sorular sorması için UI öğeleri
|
53 |
+
query_input = gr.Textbox(label="Step-1: Ask a question about the pre-uploaded PDF")
|
54 |
+
output = gr.Textbox(label="Response")
|
55 |
+
|
56 |
+
btn = gr.Button("Submit")
|
57 |
+
|
58 |
+
# Soru sorulduğunda modelin cevabını almak için
|
59 |
+
btn.click(fn=respond_to_pdf, inputs=[query_input], outputs=output)
|
60 |
+
|
61 |
+
# Uygulamayı başlat
|
62 |
if __name__ == "__main__":
|
63 |
+
demo.launch()
|