aidevhund commited on
Commit
76250b9
·
verified ·
1 Parent(s): 1cce7d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -90
app.py CHANGED
@@ -12,30 +12,12 @@ import base64
12
  # Load environment variables
13
  load_dotenv()
14
 
15
- llm_models = [
16
- "mistralai/Mixtral-8x7B-Instruct-v0.1",
17
- "meta-llama/Meta-Llama-3-8B-Instruct",
18
- "mistralai/Mistral-7B-Instruct-v0.2",
19
- "tiiuae/falcon-7b-instruct",
20
- # "mistralai/Mixtral-8x22B-Instruct-v0.1", ## 281GB>10GB
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 list
49
- selected_embed_model_name = embed_models[0] # Default to the first model in the list
50
  vector_index = None
51
 
52
  # Initialize the parser
53
  parser = LlamaParse(api_key=os.getenv("LLAMA_INDEX_API"), result_type='markdown')
54
- # Define file extractor with various common extensions
55
  file_extractor = {
56
- '.pdf': parser, # PDF documents
57
- '.docx': parser, # Microsoft Word documents
58
- '.doc': parser, # Older Microsoft Word documents
59
- '.txt': parser, # Plain text files
60
- '.csv': parser, # Comma-separated values files
61
- '.xlsx': parser, # Microsoft Excel files (requires additional processing for tables)
62
- '.pptx': parser, # Microsoft PowerPoint files (for slides)
63
- '.html': parser, # HTML files (web pages)
64
- # '.rtf': parser, # Rich Text Format files
65
- # '.odt': parser, # OpenDocument Text files
66
- # '.epub': parser, # ePub files (e-books)
67
-
68
- # Image files for OCR processing
69
- '.jpg': parser, # JPEG images
70
- '.jpeg': parser, # JPEG images
71
- '.png': parser, # PNG images
72
- # '.bmp': parser, # Bitmap images
73
- # '.tiff': parser, # TIFF images
74
- # '.tif': parser, # TIFF images (alternative extension)
75
- # '.gif': parser, # GIF images (can contain text)
76
-
77
- # Scanned documents in image formats
78
- '.webp': parser, # WebP images
79
- '.svg': parser, # SVG files (vector format, may contain embedded text)
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
- print(f"Parsing done for {file_path}")
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 = selected_model # Update the global variable
101
- # print(f"Model selected: {selected_model_name}")
102
- # return f"Model set to: {selected_model_name}"
103
 
104
 
105
- # Respond function that uses the globally set selected model
 
 
106
  def respond(message, history):
107
  try:
108
- # Initialize the LLM with the selected model
109
  llm = HuggingFaceInferenceAPI(
110
  model_name=selected_llm_model_name,
111
- contextWindow=8192, # Context window size (typically max length of the model)
112
- maxTokens=1024, # Tokens per response generation (512-1024 works well for detailed answers)
113
- temperature=0.3, # Lower temperature for more focused answers (0.2-0.4 for factual info)
114
- topP=0.9, # Top-p sampling to control diversity while retaining quality
115
- frequencyPenalty=0.5, # Slight penalty to avoid repetition
116
- presencePenalty=0.5, # Encourages exploration without digressing too much
117
  token=os.getenv("TOKEN")
118
  )
119
 
120
- # Set up the query engine with the selected LLM
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
- # Encode the images
136
- github_logo_encoded = encode_image("Images/github-logo.png")
137
- linkedin_logo_encoded = encode_image("Images/linkedin-logo.png")
138
- website_logo_encoded = encode_image("Images/ai-logo.png")
 
 
 
139
 
140
  # UI Setup
141
- with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]), css='footer {visibility: hidden}') as demo:
142
- gr.Markdown("# DocBot📄🤖")
143
  with gr.Tabs():
144
- with gr.TabItem("Intro"):
145
  gr.Markdown(md.description)
146
 
147
- with gr.TabItem("DocBot"):
148
- with gr.Accordion("=== IMPORTANT: READ ME FIRST ===", open=False):
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="Step-1: Upload document")
153
- # gr.Markdown("Dont know what to select check out in Intro tab")
154
- embed_model_dropdown = gr.Dropdown(embed_models, label="Step-2: Select Embedding", interactive=True)
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="Step-3: Select LLM", interactive=True)
160
  with gr.Column(scale=3):
161
  gr.ChatInterface(
162
  fn=respond,
163
  chatbot=gr.Chatbot(height=500),
164
- theme = "soft",
165
- show_progress='full',
166
- # cache_mode='lazy',
167
- textbox=gr.Textbox(placeholder="Step-4: Ask me questions on the uploaded document!", container=False)
168
  )
169
- gr.HTML(md.footer.format(github_logo_encoded, linkedin_logo_encoded, website_logo_encoded))
170
- # Set up Gradio interactions
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
- # Launch the demo with a public link option
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()