Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 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"
|
| 108 |
-
inputs =
|
| 109 |
with torch.no_grad():
|
| 110 |
-
outputs =
|
| 111 |
-
response =
|
| 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):
|