Sarath0x8f commited on
Commit
3e21c23
·
verified ·
1 Parent(s): bed27c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -30
app.py CHANGED
@@ -10,16 +10,34 @@ import gradio as gr
10
  # Load environment variables
11
  load_dotenv()
12
 
13
- # Initialize the LLM and parser
14
- llm = HuggingFaceInferenceAPI(
15
- # model_name="meta-llama/Meta-Llama-3-8B-Instruct",
16
- model_name="mistralai/Mixtral-8x7B-Instruct-v0.1",
17
- # model_name="nvidia/Llama3-ChatQA-1.5-70B", ## 144GB > 10GB
18
- # model_name= "Nexusflow/NexusRaven-V2-13B", ## 26 GB
19
- token=os.getenv("TOKEN"),
20
- # model_config['protected_namespaces'] = ()
21
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
 
 
 
 
23
  parser = LlamaParse(api_key=os.getenv("LLAMA_INDEX_API"), result_type='markdown')
24
  file_extractor = {'.pdf': parser, '.docx': parser, '.doc': parser}
25
 
@@ -29,32 +47,51 @@ embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
29
  # Global variable to store documents loaded from user-uploaded files
30
  vector_index = None
31
 
 
32
  # File processing function
33
  def load_files(file_path: str):
34
  try:
35
  global vector_index
36
  document = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()
37
  vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
38
- print(f"parsing done {file_path}")
39
  filename = os.path.basename(file_path)
40
- return f"Ready to give response on give {filename}"
41
  except Exception as e:
42
- return f"An error occurred {e}"
 
43
 
 
 
 
 
 
 
 
 
 
44
  def respond(message, history):
45
  try:
 
 
 
 
 
 
 
 
 
 
46
  query_engine = vector_index.as_query_engine(llm=llm)
47
  bot_message = query_engine.query(message)
48
- # output = ""
49
- # for chr in bot_message:
50
- # output += chr
51
- # yield output
52
- print(f"\n{datetime.now()}:: {message} --> {str(bot_message)}\n")
53
- return str(bot_message)
54
  except Exception as e:
55
- if e == "'NoneType' object has no attribute 'as_query_engine'":
56
- return "upload file"
57
- return f"an error occurred {e}"
 
58
 
59
  # UI Setup
60
  with gr.Blocks() as demo:
@@ -65,18 +102,20 @@ with gr.Blocks() as demo:
65
  clear = gr.ClearButton()
66
  btn = gr.Button("Submit", variant='primary')
67
  output = gr.Text(label='Vector Index')
 
 
68
  with gr.Column(scale=3):
69
- gr.ChatInterface(fn=respond,
70
- chatbot=gr.Chatbot(height=500),
71
- textbox=gr.Textbox(placeholder="Ask me questions on given document!", container=False, scale=7),
72
- # examples=["summarize the document"]
73
- )
74
 
75
- # Action on button click to process file and load into index
 
76
  btn.click(fn=load_files, inputs=file_input, outputs=output)
77
- clear.click(lambda: [None]*2, outputs=[file_input, output])
78
-
79
 
80
- # Launch the demo with public link option
81
  if __name__ == "__main__":
82
  demo.launch()
 
10
  # Load environment variables
11
  load_dotenv()
12
 
13
+ models = [
14
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
15
+ "meta-llama/Meta-Llama-3-8B-Instruct",
16
+ # "NousResearch/Yarn-Mistral-7b-64k", ## 14GB>10GB
17
+ # "impira/layoutlm-document-qa", ## ERR
18
+ # "Qwen/Qwen1.5-7B", ## 15GB
19
+ # "Qwen/Qwen2.5-3B", ## high response time
20
+ # "google/gemma-2-2b-jpn-it", ## high response time
21
+ # "impira/layoutlm-invoices", ## bad req
22
+ # "google/pix2struct-docvqa-large", ## bad req
23
+ "mistralai/Mistral-7B-Instruct-v0.2",
24
+ # "google/gemma-7b-it", ## 17GB > 10GB
25
+ # "google/gemma-2b-it", ## high response time
26
+ # "HuggingFaceH4/zephyr-7b-beta", ## high response time
27
+ # "HuggingFaceH4/zephyr-7b-gemma-v0.1", ## bad req
28
+ # "microsoft/phi-2", ## high response time
29
+ # "TinyLlama/TinyLlama-1.1B-Chat-v1.0", ## high response time
30
+ # "mosaicml/mpt-7b-instruct", ## 13GB>10GB
31
+ "tiiuae/falcon-7b-instruct",
32
+ "google/flan-t5-xxl"
33
+ # "NousResearch/Yarn-Mistral-7b-128k", ## 14GB>10GB
34
+ # "Qwen/Qwen2.5-7B-Instruct", ## 15GB>10GB
35
+ ]
36
 
37
+ # Global variable for selected model
38
+ selected_model_name = models[0] # Default to the first model in the list
39
+
40
+ # Initialize the parser
41
  parser = LlamaParse(api_key=os.getenv("LLAMA_INDEX_API"), result_type='markdown')
42
  file_extractor = {'.pdf': parser, '.docx': parser, '.doc': parser}
43
 
 
47
  # Global variable to store documents loaded from user-uploaded files
48
  vector_index = None
49
 
50
+
51
  # File processing function
52
  def load_files(file_path: str):
53
  try:
54
  global vector_index
55
  document = SimpleDirectoryReader(input_files=[file_path], file_extractor=file_extractor).load_data()
56
  vector_index = VectorStoreIndex.from_documents(document, embed_model=embed_model)
57
+ print(f"Parsing done for {file_path}")
58
  filename = os.path.basename(file_path)
59
+ return f"Ready to give response on {filename}"
60
  except Exception as e:
61
+ return f"An error occurred: {e}"
62
+
63
 
64
+ # Function to handle the selected model from dropdown
65
+ def set_model(selected_model):
66
+ global selected_model_name
67
+ selected_model_name = selected_model # Update the global variable
68
+ # print(f"Model selected: {selected_model_name}")
69
+ # return f"Model set to: {selected_model_name}"
70
+
71
+
72
+ # Respond function that uses the globally set selected model
73
  def respond(message, history):
74
  try:
75
+ # Initialize the LLM with the selected model
76
+ llm = HuggingFaceInferenceAPI(
77
+ model_name=selected_model_name,
78
+ token=os.getenv("TOKEN")
79
+ )
80
+
81
+ # Check selected model
82
+ # print(f"Using model: {selected_model_name}")
83
+
84
+ # Set up the query engine with the selected LLM
85
  query_engine = vector_index.as_query_engine(llm=llm)
86
  bot_message = query_engine.query(message)
87
+
88
+ print(f"\n{datetime.now()}:{selected_model_name}:: {message} --> {str(bot_message)}\n")
89
+ return f"{selected_model_name}:\n{str(bot_message)}"
 
 
 
90
  except Exception as e:
91
+ if str(e) == "'NoneType' object has no attribute 'as_query_engine'":
92
+ return "Please upload a file."
93
+ return f"An error occurred: {e}"
94
+
95
 
96
  # UI Setup
97
  with gr.Blocks() as demo:
 
102
  clear = gr.ClearButton()
103
  btn = gr.Button("Submit", variant='primary')
104
  output = gr.Text(label='Vector Index')
105
+ model_dropdown = gr.Dropdown(models, label="Select Model", interactive=True)
106
+
107
  with gr.Column(scale=3):
108
+ gr.ChatInterface(
109
+ fn=respond,
110
+ chatbot=gr.Chatbot(height=500),
111
+ textbox=gr.Textbox(placeholder="Ask me questions on the uploaded document!", container=False, scale=7)
112
+ )
113
 
114
+ # Set up Gradio interactions
115
+ model_dropdown.change(fn=set_model, inputs=model_dropdown)
116
  btn.click(fn=load_files, inputs=file_input, outputs=output)
117
+ clear.click(lambda: [None] * 2, outputs=[file_input, output])
 
118
 
119
+ # Launch the demo with a public link option
120
  if __name__ == "__main__":
121
  demo.launch()