aidevhund commited on
Commit
1958913
·
verified ·
1 Parent(s): b45902d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -92
app.py CHANGED
@@ -1,95 +1,33 @@
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
- "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
- ]
21
-
22
- embed_models = [
23
- "BAAI/bge-small-en-v1.5", # 33.4M
24
- "NeuML/pubmedbert-base-embeddings",
25
- "BAAI/llm-embedder", # 109M
26
- "BAAI/bge-large-en" # 335M
27
- ]
28
-
29
- # Global variable for selected model
30
- selected_llm_model_name = llm_models[0] # Default to the first model in the list
31
- selected_embed_model_name = embed_models[0] # Default to the first model in the list
32
- vector_index = None
33
-
34
- # Initialize the parser
35
- parser = LlamaParse(api_key=os.getenv("LLAMA_INDEX_API"), result_type='markdown')
36
- # Define file extractor with various common extensions
37
- file_extractor = {
38
- '.pdf': parser, # PDF documents
39
- '.docx': parser, # Microsoft Word documents
40
- '.doc': parser, # Older Microsoft Word documents
41
- '.txt': parser, # Plain text files
42
- '.csv': parser, # Comma-separated values files
43
- '.xlsx': parser, # Microsoft Excel files (requires additional processing for tables)
44
- '.pptx': parser, # Microsoft PowerPoint files (for slides)
45
- '.html': parser, # HTML files (web pages)
46
-
47
- # Image files for OCR processing
48
- '.jpg': parser, # JPEG images
49
- '.jpeg': parser, # JPEG images
50
- '.png': parser, # PNG images
51
-
52
-
53
- # Scanned documents in image formats
54
- '.webp': parser, # WebP images
55
- '.svg': parser, # SVG files (vector format, may contain embedded text)
56
  }
57
 
 
 
58
 
59
- # File processing function
60
- def load_files(file_path: str, embed_model_name: str):
61
- try:
62
- global vector_index
63
- document = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()
64
- embed_model = HuggingFaceEmbedding(model_name=embed_model_name)
65
- vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
66
- print(f"Parsing done for {file_path}")
67
- filename = os.path.basename(file_path)
68
- return f"Ready to give response on {filename}"
69
- except Exception as e:
70
- return f"An error occurred: {e}"
71
-
72
-
73
- # Function to handle the selected model from dropdown
74
- def set_llm_model(selected_model):
75
  global selected_llm_model_name
76
- selected_llm_model_name = selected_model # Update the global variable
77
- # print(f"Model selected: {selected_model_name}")
78
- # return f"Model set to: {selected_model_name}"
79
-
80
 
81
- # Respond function that uses the globally set selected model
82
  def respond(message, history):
83
  try:
84
  # Initialize the LLM with the selected model
85
  llm = HuggingFaceInferenceAPI(
86
- model_name=selected_llm_model_name,
87
- contextWindow=8192, # Context window size (typically max length of the model)
88
- maxTokens=1024, # Tokens per response generation (512-1024 works well for detailed answers)
89
- temperature=0.3, # Lower temperature for more focused answers (0.2-0.4 for factual info)
90
- topP=0.9, # Top-p sampling to control diversity while retaining quality
91
- frequencyPenalty=0.5, # Slight penalty to avoid repetition
92
- presencePenalty=0.5, # Encourages exploration without digressing too much
93
  token=os.getenv("TOKEN")
94
  )
95
 
@@ -104,10 +42,6 @@ def respond(message, history):
104
  return "Please upload a file."
105
  return f"An error occurred: {e}"
106
 
107
- def encode_image(image_path):
108
- with open(image_path, "rb") as image_file:
109
- return base64.b64encode(image_file.read()).decode('utf-8')
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("")
@@ -121,20 +55,24 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
121
  with gr.Row():
122
  with gr.Column(scale=1):
123
  file_input = gr.File(file_count="single", type='filepath', label="Upload document")
124
- # gr.Markdown("Dont know what to select check out in Intro tab")
125
  embed_model_dropdown = gr.Dropdown(embed_models, label="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="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="Ask me any questions on the uploaded document!", container=False)
139
  )
140
 
@@ -145,4 +83,4 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
145
 
146
  # Launch the demo with a public link option
147
  if __name__ == "__main__":
148
- demo.launch()
 
1
+ # Mapping for display names and actual model names
2
+ llm_display_names = {
3
+ "tiiuae/falcon-7b-instruct": "HundAI",
4
+ "mistralai/Mixtral-8x7B-Instruct-v0.1": "Mixtral-8x7B",
5
+ "meta-llama/Meta-Llama-3-8B-Instruct": "Meta-Llama-3",
6
+ "mistralai/Mistral-7B-Instruct-v0.2": "Mistral-7B",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  }
8
 
9
+ # Reverse mapping to retrieve original names
10
+ llm_reverse_mapping = {v: k for k, v in llm_display_names.items()}
11
 
12
+ # Update UI to use display names
13
+ def set_llm_model(display_name):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  global selected_llm_model_name
15
+ # Retrieve the original model name using the reverse mapping
16
+ selected_llm_model_name = llm_reverse_mapping.get(display_name, display_name)
17
+ print(f"Model selected: {selected_llm_model_name}")
 
18
 
19
+ # Respond function remains unchanged
20
  def respond(message, history):
21
  try:
22
  # Initialize the LLM with the selected model
23
  llm = HuggingFaceInferenceAPI(
24
+ model_name=selected_llm_model_name, # Use the backend model name
25
+ contextWindow=8192,
26
+ maxTokens=1024,
27
+ temperature=0.3,
28
+ topP=0.9,
29
+ frequencyPenalty=0.5,
30
+ presencePenalty=0.5,
31
  token=os.getenv("TOKEN")
32
  )
33
 
 
42
  return "Please upload a file."
43
  return f"An error occurred: {e}"
44
 
 
 
 
 
45
  # UI Setup
46
  with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]), css='footer {visibility: hidden}') as demo:
47
  gr.Markdown("")
 
55
  with gr.Row():
56
  with gr.Column(scale=1):
57
  file_input = gr.File(file_count="single", type='filepath', label="Upload document")
 
58
  embed_model_dropdown = gr.Dropdown(embed_models, label="Select Embedding", interactive=True)
59
  with gr.Row():
60
  btn = gr.Button("Submit", variant='primary')
61
  clear = gr.ClearButton()
62
  output = gr.Text(label='Vector Index')
63
+
64
+ # Use display names for LLM dropdown
65
+ llm_model_dropdown = gr.Dropdown(
66
+ list(llm_display_names.values()), # Display names
67
+ label="Select LLM",
68
+ interactive=True
69
+ )
70
+
71
  with gr.Column(scale=3):
72
  gr.ChatInterface(
73
  fn=respond,
74
  chatbot=gr.Chatbot(height=500),
 
75
  show_progress='full',
 
76
  textbox=gr.Textbox(placeholder="Ask me any questions on the uploaded document!", container=False)
77
  )
78
 
 
83
 
84
  # Launch the demo with a public link option
85
  if __name__ == "__main__":
86
+ demo.launch()