Abbeite commited on
Commit
f3cacbe
1 Parent(s): a9a4fe5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -23
app.py CHANGED
@@ -1,30 +1,41 @@
1
  import streamlit as st
2
- from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
 
3
 
4
- # Streamlit's cache decorator to cache the model and tokenizer loading
 
 
 
 
 
 
5
 
6
- def load_pipeline():
7
- model_name = "NousResearch/Llama-2-7b-chat-hf" # Replace with your actual model name
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- model = AutoModelForCausalLM.from_pretrained(model_name)
10
- chat_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=300)
11
- return chat_pipeline
12
 
13
- # Initialize the pipeline
14
- chat_pipeline = load_pipeline()
 
 
15
 
16
- st.title("Interact with Your Model")
 
 
17
 
18
- # User input
19
- user_input = st.text_area("Enter your prompt:", "")
 
 
 
 
 
 
20
 
21
- if st.button("Submit"):
22
- if user_input:
23
- try:
24
- # Generate text based on the input
25
- generated_text = chat_pipeline(user_input)[0]['generated_text']
26
- st.write(generated_text)
27
- except Exception as e:
28
- st.error(f"Error generating text: {e}")
29
- else:
30
- st.write("Please enter a prompt.")
 
1
  import streamlit as st
2
+ import fitz # PyMuPDF
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
 
5
+ # Function to read and extract text from a PDF document
6
+ def read_pdf(file_path):
7
+ text = ""
8
+ with fitz.open(file_path) as doc:
9
+ for page in doc:
10
+ text += page.get_text()
11
+ return text
12
 
13
+ # Load the document text
14
+ document_text = read_pdf("jeff_wo.pdf") # Adjust path to your PDF file
 
 
 
 
15
 
16
+ # Streamlit UI
17
+ st.title("LLaMA 2-based Q&A System")
18
+ st.write("### Enter your query below:")
19
+ query = st.text_input("Query")
20
 
21
+ # Load tokenizer and model
22
+ tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
23
+ model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
24
 
25
+ # Function to get answers using the LLaMA 2 model
26
+ def get_answer(context, query):
27
+ input_text = f"Context: {context}\nQ: {query}\nA:"
28
+ input_ids = tokenizer.encode(input_text, return_tensors="pt")
29
+ # Generate an answer to the query
30
+ output = model.generate(input_ids, max_length=512, num_return_sequences=1)
31
+ answer = tokenizer.decode(output[0], skip_special_tokens=True)
32
+ return answer
33
 
34
+ # Button to generate answers
35
+ if st.button("Get Answer"):
36
+ with st.spinner("Finding the answer..."):
37
+ answer = get_answer(document_text, query)
38
+ st.write("### Answer:")
39
+ st.write(answer)
40
+
41
+ # Ensure to replace `path/to/your/document.pdf` with the actual path to the PDF in your repo