Sarath0x8f commited on
Commit
90dc0e0
·
verified ·
1 Parent(s): abf8cd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -93
app.py CHANGED
@@ -6,7 +6,7 @@ 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
@@ -17,78 +17,38 @@ llm_models = [
17
  "meta-llama/Meta-Llama-3-8B-Instruct",
18
  "mistralai/Mistral-7B-Instruct-v0.2",
19
  "tiiuae/falcon-7b-instruct",
20
- # "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
21
- # "deepseek-ai/deepseek-vl2", ## 54GB > 10GB
22
- # "deepseek-ai/deepseek-vl2-small", ## 32GB > 10GB
23
- # "deepseek-ai/deepseek-vl2-tiny", ## high response time
24
- # "deepseek-ai/deepseek-llm-7b-chat", ## 13GB > 10GB
25
- # "deepseek-ai/deepseek-math-7b-instruct", ## 13GB > 10GB
26
- # "deepseek-ai/deepseek-coder-33b-instruct", ## 66GB > 10GB
27
- # "deepseek-ai/DeepSeek-R1-Zero", ## 688GB > 10GB
28
- # "mistralai/Mixtral-8x22B-Instruct-v0.1", ## 281GB>10GB
29
- # "NousResearch/Yarn-Mistral-7b-64k", ## 14GB>10GB
30
- # "impira/layoutlm-document-qa", ## ERR
31
- # "Qwen/Qwen1.5-7B", ## 15GB
32
- # "Qwen/Qwen2.5-3B", ## high response time
33
- # "google/gemma-2-2b-jpn-it", ## high response time
34
- # "impira/layoutlm-invoices", ## bad req
35
- # "google/pix2struct-docvqa-large", ## bad req
36
- # "google/gemma-7b-it", ## 17GB > 10GB
37
- # "google/gemma-2b-it", ## high response time
38
- # "HuggingFaceH4/zephyr-7b-beta", ## high response time
39
- # "HuggingFaceH4/zephyr-7b-gemma-v0.1", ## bad req
40
- # "microsoft/phi-2", ## high response time
41
- # "TinyLlama/TinyLlama-1.1B-Chat-v1.0", ## high response time
42
- # "mosaicml/mpt-7b-instruct", ## 13GB>10GB
43
- # "google/flan-t5-xxl" ## high respons time
44
- # "NousResearch/Yarn-Mistral-7b-128k", ## 14GB>10GB
45
- # "Qwen/Qwen2.5-7B-Instruct", ## 15GB>10GB
46
  ]
47
 
48
  embed_models = [
49
- "BAAI/bge-small-en-v1.5", # 33.4M
50
  "NeuML/pubmedbert-base-embeddings",
51
- "BAAI/llm-embedder", # 109M
52
- "BAAI/bge-large-en" # 335M
53
  ]
54
 
55
  # Global variable for selected model
56
- selected_llm_model_name = llm_models[0] # Default to the first model in the list
57
- selected_embed_model_name = embed_models[0] # Default to the first model in the list
58
  vector_index = None
59
 
60
  # Initialize the parser
61
  parser = LlamaParse(api_key=os.getenv("LLAMA_INDEX_API"), result_type='markdown')
62
- # Define file extractor with various common extensions
63
  file_extractor = {
64
- '.pdf': parser, # PDF documents
65
- '.docx': parser, # Microsoft Word documents
66
- '.doc': parser, # Older Microsoft Word documents
67
- '.txt': parser, # Plain text files
68
- '.csv': parser, # Comma-separated values files
69
- '.xlsx': parser, # Microsoft Excel files (requires additional processing for tables)
70
- '.pptx': parser, # Microsoft PowerPoint files (for slides)
71
- '.html': parser, # HTML files (web pages)
72
- # '.rtf': parser, # Rich Text Format files
73
- # '.odt': parser, # OpenDocument Text files
74
- # '.epub': parser, # ePub files (e-books)
75
-
76
- # Image files for OCR processing
77
- '.jpg': parser, # JPEG images
78
- '.jpeg': parser, # JPEG images
79
- '.png': parser, # PNG images
80
- # '.bmp': parser, # Bitmap images
81
- # '.tiff': parser, # TIFF images
82
- # '.tif': parser, # TIFF images (alternative extension)
83
- # '.gif': parser, # GIF images (can contain text)
84
-
85
- # Scanned documents in image formats
86
- '.webp': parser, # WebP images
87
- '.svg': parser, # SVG files (vector format, may contain embedded text)
88
  }
89
 
90
-
91
- # File processing function
92
  def load_files(file_path: str, embed_model_name: str):
93
  try:
94
  global vector_index
@@ -101,53 +61,43 @@ def load_files(file_path: str, embed_model_name: str):
101
  except Exception as e:
102
  return f"An error occurred: {e}"
103
 
104
-
105
- # Function to handle the selected model from dropdown
106
  def set_llm_model(selected_model):
107
  global selected_llm_model_name
108
- selected_llm_model_name = selected_model # Update the global variable
109
- # print(f"Model selected: {selected_model_name}")
110
- # return f"Model set to: {selected_model_name}"
111
-
112
 
113
- # Respond function that uses the globally set selected model
114
  def respond(message, history):
115
  try:
116
- # Initialize the LLM with the selected model
117
  llm = HuggingFaceInferenceAPI(
118
  model_name=selected_llm_model_name,
119
- contextWindow=8192, # Context window size (typically max length of the model)
120
- maxTokens=1024, # Tokens per response generation (512-1024 works well for detailed answers)
121
- temperature=0.3, # Lower temperature for more focused answers (0.2-0.4 for factual info)
122
- topP=0.9, # Top-p sampling to control diversity while retaining quality
123
- frequencyPenalty=0.5, # Slight penalty to avoid repetition
124
- presencePenalty=0.5, # Encourages exploration without digressing too much
125
  token=os.getenv("TOKEN")
126
  )
127
-
128
- # Set up the query engine with the selected LLM
129
- query_engine = vector_index.as_query_engine(llm=llm)
130
- bot_message = query_engine.query(message)
131
-
132
- print(f"\n{datetime.now()}:{selected_llm_model_name}:: {message} --> {str(bot_message)}\n")
133
- return f"{selected_llm_model_name}:\n{str(bot_message)}"
134
- except Exception as e:
135
- if str(e) == "'NoneType' object has no attribute 'as_query_engine'":
136
  return "Please upload a file."
 
137
  return f"An error occurred: {e}"
138
 
139
  def encode_image(image_path):
140
  with open(image_path, "rb") as image_file:
141
  return base64.b64encode(image_file.read()).decode('utf-8')
142
 
143
- # Encode the images
144
  github_logo_encoded = encode_image("Images/github-logo.png")
145
  linkedin_logo_encoded = encode_image("Images/linkedin-logo.png")
146
  website_logo_encoded = encode_image("Images/ai-logo.png")
147
 
148
- # UI Setup
149
  with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]), css='footer {visibility: hidden}') as demo:
150
- gr.Markdown("# DocBot📄🤖")
151
  with gr.Tabs():
152
  with gr.TabItem("Intro"):
153
  gr.Markdown(md.description)
@@ -158,7 +108,6 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
158
  with gr.Row():
159
  with gr.Column(scale=1):
160
  file_input = gr.File(file_count="single", type='filepath', label="Step-1: Upload document")
161
- # gr.Markdown("Dont know what to select check out in Intro tab")
162
  embed_model_dropdown = gr.Dropdown(embed_models, label="Step-2: Select Embedding", interactive=True)
163
  with gr.Row():
164
  btn = gr.Button("Submit", variant='primary')
@@ -169,17 +118,13 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
169
  gr.ChatInterface(
170
  fn=respond,
171
  chatbot=gr.Chatbot(height=500),
172
- theme = "soft",
173
- show_progress='full',
174
- # cache_mode='lazy',
175
  textbox=gr.Textbox(placeholder="Step-4: Ask me questions on the uploaded document!", container=False)
176
  )
177
  gr.HTML(md.footer.format(github_logo_encoded, linkedin_logo_encoded, website_logo_encoded))
178
- # Set up Gradio interactions
179
- llm_model_dropdown.change(fn=set_llm_model, inputs=llm_model_dropdown)
180
  btn.click(fn=load_files, inputs=[file_input, embed_model_dropdown], outputs=output)
181
  clear.click(lambda: [None] * 3, outputs=[file_input, embed_model_dropdown, output])
182
 
183
- # Launch the demo with a public link option
184
  if __name__ == "__main__":
185
- demo.launch(share=True)
 
6
  import os
7
  from dotenv import load_dotenv
8
  import gradio as gr
9
+ import markdown as md
10
  import base64
11
 
12
  # Load environment variables
 
17
  "meta-llama/Meta-Llama-3-8B-Instruct",
18
  "mistralai/Mistral-7B-Instruct-v0.2",
19
  "tiiuae/falcon-7b-instruct",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  ]
21
 
22
  embed_models = [
23
+ "BAAI/bge-small-en-v1.5",
24
  "NeuML/pubmedbert-base-embeddings",
25
+ "BAAI/llm-embedder",
26
+ "BAAI/bge-large-en"
27
  ]
28
 
29
  # Global variable for selected model
30
+ selected_llm_model_name = llm_models[0]
31
+ selected_embed_model_name = embed_models[0]
32
  vector_index = None
33
 
34
  # Initialize the parser
35
  parser = LlamaParse(api_key=os.getenv("LLAMA_INDEX_API"), result_type='markdown')
 
36
  file_extractor = {
37
+ '.pdf': parser,
38
+ '.docx': parser,
39
+ '.doc': 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
  def load_files(file_path: str, embed_model_name: str):
53
  try:
54
  global vector_index
 
61
  except Exception as e:
62
  return f"An error occurred: {e}"
63
 
 
 
64
  def set_llm_model(selected_model):
65
  global selected_llm_model_name
66
+ selected_llm_model_name = selected_model
67
+ return f"Model set to: {selected_model}"
 
 
68
 
 
69
  def respond(message, history):
70
  try:
 
71
  llm = HuggingFaceInferenceAPI(
72
  model_name=selected_llm_model_name,
73
+ contextWindow=8192,
74
+ maxTokens=1024,
75
+ temperature=0.3,
76
+ topP=0.9,
77
+ frequencyPenalty=0.5,
78
+ presencePenalty=0.5,
79
  token=os.getenv("TOKEN")
80
  )
81
+ if vector_index is not None:
82
+ query_engine = vector_index.as_query_engine(llm=llm)
83
+ bot_message = query_engine.query(message)
84
+ print(f"\n{datetime.now()}:{selected_llm_model_name}:: {message} --> {str(bot_message)}\n")
85
+ return f"{selected_llm_model_name}:\n{str(bot_message)}"
86
+ else:
 
 
 
87
  return "Please upload a file."
88
+ except Exception as e:
89
  return f"An error occurred: {e}"
90
 
91
  def encode_image(image_path):
92
  with open(image_path, "rb") as image_file:
93
  return base64.b64encode(image_file.read()).decode('utf-8')
94
 
 
95
  github_logo_encoded = encode_image("Images/github-logo.png")
96
  linkedin_logo_encoded = encode_image("Images/linkedin-logo.png")
97
  website_logo_encoded = encode_image("Images/ai-logo.png")
98
 
 
99
  with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]), css='footer {visibility: hidden}') as demo:
100
+ gr.Markdown("# DocBot")
101
  with gr.Tabs():
102
  with gr.TabItem("Intro"):
103
  gr.Markdown(md.description)
 
108
  with gr.Row():
109
  with gr.Column(scale=1):
110
  file_input = gr.File(file_count="single", type='filepath', label="Step-1: Upload document")
 
111
  embed_model_dropdown = gr.Dropdown(embed_models, label="Step-2: Select Embedding", interactive=True)
112
  with gr.Row():
113
  btn = gr.Button("Submit", variant='primary')
 
118
  gr.ChatInterface(
119
  fn=respond,
120
  chatbot=gr.Chatbot(height=500),
121
+ theme="soft",
 
 
122
  textbox=gr.Textbox(placeholder="Step-4: Ask me questions on the uploaded document!", container=False)
123
  )
124
  gr.HTML(md.footer.format(github_logo_encoded, linkedin_logo_encoded, website_logo_encoded))
125
+ llm_model_dropdown.change(fn=set_llm_model, inputs=llm_model_dropdown, outputs=[gr.Text(label="Model selected")])
 
126
  btn.click(fn=load_files, inputs=[file_input, embed_model_dropdown], outputs=output)
127
  clear.click(lambda: [None] * 3, outputs=[file_input, embed_model_dropdown, output])
128
 
 
129
  if __name__ == "__main__":
130
+ demo.launch()