VDNT11 commited on
Commit
80d1ff8
·
verified ·
1 Parent(s): b865f52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -11
app.py CHANGED
@@ -12,18 +12,16 @@ from langchain.text_splitter import RecursiveCharacterTextSplitter
12
  from langchain.docstore.document import Document
13
  import PyPDF2
14
  import tempfile
15
- from bitsandbytes import functional as F
16
- import bitsandbytes as bnb
17
 
18
  # Initialize BLIP for image captioning
19
  blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
20
  blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large").to("cuda" if torch.cuda.is_available() else "cpu")
21
 
22
- # Initialize Mistral-7B for conversational tasks
23
- mistral_model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1"
24
- mistral_tokenizer = AutoTokenizer.from_pretrained(mistral_model_name)
25
- mistral_model = AutoModelForCausalLM.from_pretrained(
26
- mistral_model_name,
27
  load_in_4bit=True,
28
  device_map="auto",
29
  torch_dtype=torch.bfloat16
@@ -104,11 +102,11 @@ def chat_with_llm(message, history):
104
  if vector_store:
105
  docs = vector_store.similarity_search(message, k=3)
106
  context = "\n".join([doc.page_content for doc in docs])
107
- prompt = f"[INST] You are a helpful assistant. Use the following context to answer the question accurately:\n\n{context}\n\nQuestion: {message} [/INST]"
108
- inputs = mistral_tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
109
  with torch.no_grad():
110
- outputs = mistral_model.generate(**inputs, max_length=1000, num_return_sequences=1, temperature=0.7)
111
- response = mistral_tokenizer.decode(outputs[0], skip_special_tokens=True)
112
  return response.replace(prompt, "").strip()
113
 
114
  def image_tab(image, target_languages):
 
12
  from langchain.docstore.document import Document
13
  import PyPDF2
14
  import tempfile
 
 
15
 
16
  # Initialize BLIP for image captioning
17
  blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
18
  blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large").to("cuda" if torch.cuda.is_available() else "cpu")
19
 
20
+ # Initialize LLaMA-3-8B-Instruct for conversational tasks
21
+ llama_model_name = "meta-llama/Meta-Llama-3-8B-Instruct"
22
+ llama_tokenizer = AutoTokenizer.from_pretrained(llama_model_name)
23
+ llama_model = AutoModelForCausalLM.from_pretrained(
24
+ llama_model_name,
25
  load_in_4bit=True,
26
  device_map="auto",
27
  torch_dtype=torch.bfloat16
 
102
  if vector_store:
103
  docs = vector_store.similarity_search(message, k=3)
104
  context = "\n".join([doc.page_content for doc in docs])
105
+ prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id>You are a helpful assistant. Use the following context to answer the question accurately:\n\n{context}<|eot_id><|start_header_id|>user<|end_header_id>{message}<|eot_id><|start_header_id|>assistant<|end_header_id>"
106
+ inputs = llama_tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
107
  with torch.no_grad():
108
+ outputs = llama_model.generate(**inputs, max_length=1000, num_return_sequences=1, temperature=0.7)
109
+ response = llama_tokenizer.decode(outputs[0], skip_special_tokens=True)
110
  return response.replace(prompt, "").strip()
111
 
112
  def image_tab(image, target_languages):