Chan-Y commited on
Commit
c9f19e7
·
verified ·
1 Parent(s): 02285d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- from llama_index.core.readers import SimpleDirectoryReader
3
  from llama_index.core import VectorStoreIndex, Document
4
  from llama_index.core.node_parser import SentenceSplitter
5
  from llama_index.core import Settings
@@ -8,14 +7,15 @@ from llama_index.llms.huggingface import HuggingFaceLLM
8
  import csv
9
  from docx import Document as DocxDocument
10
  import fitz
11
-
12
- # Define the list of LLMs with their names and models
 
 
13
  lm_list = {
14
  "google/gemma-2-9b-it": "google/gemma-2-9b-it",
15
  "mistralai/Mistral-7B-Instruct-v0.3": "mistralai/Mistral-7B-Instruct-v0.3"
16
  }
17
 
18
- # Initialize the query engine globally
19
  query_engine = None
20
 
21
  def process_file(file):
@@ -48,13 +48,14 @@ def process_file(file):
48
 
49
  def handle_file_upload(file, llm_name):
50
  global query_engine
51
-
 
52
  Settings.llm = HuggingFaceLLM(model_name=llm_name)
53
 
54
  documents = process_file(file)
55
 
56
  text_splitter = SentenceSplitter(chunk_size=512, chunk_overlap=10)
57
- Settings.embed_model = HuggingFaceEmbedding(model_name="nomic-embed-text:latest")
58
  Settings.text_splitter = text_splitter
59
  index = VectorStoreIndex.from_documents(
60
  documents, transformations=[text_splitter], embed_model=Settings.embed_model
@@ -79,4 +80,4 @@ gr.Interface(
79
  title="Document Question Answering",
80
  description="Upload a document and choose a language model to get answers.",
81
  allow_flagging=False
82
- ).launch()
 
1
  import gradio as gr
 
2
  from llama_index.core import VectorStoreIndex, Document
3
  from llama_index.core.node_parser import SentenceSplitter
4
  from llama_index.core import Settings
 
7
  import csv
8
  from docx import Document as DocxDocument
9
  import fitz
10
+ import os
11
+ import torch
12
+ os.environ['CUDA_LAUNCH_BLOCKING']="1"
13
+ os.environ['PYTORCH_USE_CUDA_DSA'] = "1"
14
  lm_list = {
15
  "google/gemma-2-9b-it": "google/gemma-2-9b-it",
16
  "mistralai/Mistral-7B-Instruct-v0.3": "mistralai/Mistral-7B-Instruct-v0.3"
17
  }
18
 
 
19
  query_engine = None
20
 
21
  def process_file(file):
 
48
 
49
  def handle_file_upload(file, llm_name):
50
  global query_engine
51
+ if torch.cuda.is_available():
52
+ torch.cuda.empty_cache()
53
  Settings.llm = HuggingFaceLLM(model_name=llm_name)
54
 
55
  documents = process_file(file)
56
 
57
  text_splitter = SentenceSplitter(chunk_size=512, chunk_overlap=10)
58
+ Settings.embed_model = HuggingFaceEmbedding(model_name="nomic-ai/nomic-embed-text-v1.5", trust_remote_code=True)
59
  Settings.text_splitter = text_splitter
60
  index = VectorStoreIndex.from_documents(
61
  documents, transformations=[text_splitter], embed_model=Settings.embed_model
 
80
  title="Document Question Answering",
81
  description="Upload a document and choose a language model to get answers.",
82
  allow_flagging=False
83
+ ).launch()