g0th commited on
Commit
d91a205
·
verified ·
1 Parent(s): b2ba8cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -20
app.py CHANGED
@@ -3,15 +3,14 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  import PyPDF2
4
  import torch
5
 
6
- st.set_page_config(page_title="Perplexity-style Q&A (Mistral)", layout="wide")
7
- st.title("🧠 Perplexity-style AI Study Assistant using Mistral 7B")
8
 
9
- # Load Mistral model and tokenizer
10
  @st.cache_resource
11
  def load_model():
12
- tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
13
  model = AutoModelForCausalLM.from_pretrained(
14
- "mistralai/Mistral-7B-Instruct-v0.1",
15
  torch_dtype=torch.float16,
16
  device_map="auto"
17
  )
@@ -20,17 +19,11 @@ def load_model():
20
 
21
  textgen = load_model()
22
 
23
- # Extract text from uploaded PDF
24
  def extract_text_from_pdf(file):
25
  reader = PyPDF2.PdfReader(file)
26
- text = ""
27
- for page in reader.pages:
28
- text += page.extract_text() + "\n"
29
- return text.strip()
30
 
31
- # UI Layout
32
  query = st.text_input("Ask a question or enter a topic:")
33
-
34
  uploaded_file = st.file_uploader("Or upload a PDF to use as context:", type=["pdf"])
35
 
36
  context = ""
@@ -39,12 +32,9 @@ if uploaded_file:
39
  st.text_area("📄 Extracted PDF Text", context, height=200)
40
 
41
  if st.button("Generate Answer"):
42
- with st.spinner("Generating answer with Mistral 7B..."):
43
- prompt = query
44
- if context:
45
- prompt = f"[INST] Use the following context to answer the question:\n\n{context}\n\nQuestion: {query} [/INST]"
46
- else:
47
- prompt = f"[INST] {query} [/INST]"
48
- output = textgen(prompt)[0]["generated_text"]
49
  st.success("Answer:")
50
- st.write(output.replace(prompt, "").strip())
 
3
  import PyPDF2
4
  import torch
5
 
6
+ st.set_page_config(page_title="Perplexity-style Q&A (OpenHermes)", layout="wide")
7
+ st.title("🧠 Perplexity-style Study Assistant with OpenHermes-2.5")
8
 
 
9
  @st.cache_resource
10
  def load_model():
11
+ tokenizer = AutoTokenizer.from_pretrained("teknium/OpenHermes-2.5-Mistral-7B")
12
  model = AutoModelForCausalLM.from_pretrained(
13
+ "teknium/OpenHermes-2.5-Mistral-7B",
14
  torch_dtype=torch.float16,
15
  device_map="auto"
16
  )
 
19
 
20
  textgen = load_model()
21
 
 
22
  def extract_text_from_pdf(file):
23
  reader = PyPDF2.PdfReader(file)
24
+ return "\n".join([p.extract_text() for p in reader.pages if p.extract_text()])
 
 
 
25
 
 
26
  query = st.text_input("Ask a question or enter a topic:")
 
27
  uploaded_file = st.file_uploader("Or upload a PDF to use as context:", type=["pdf"])
28
 
29
  context = ""
 
32
  st.text_area("📄 Extracted PDF Text", context, height=200)
33
 
34
  if st.button("Generate Answer"):
35
+ with st.spinner("Generating answer..."):
36
+ prompt = f"<|system|>You are a helpful study assistant.<|user|>Context:\n{context}\n\nQuestion: {query}<|assistant|>"
37
+ result = textgen(prompt)[0]["generated_text"]
38
+ answer = result.replace(prompt, "").strip()
 
 
 
39
  st.success("Answer:")
40
+ st.write(answer)