g0th commited on
Commit
8752417
·
verified ·
1 Parent(s): 5264555

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -3,15 +3,15 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  import PyPDF2
4
  import torch
5
 
6
- st.set_page_config(page_title="Perplexity Clone (Gemma)", layout="wide")
7
- st.title("📚 Perplexity-Style AI Study Assistant using Gemma")
8
 
9
- # Load Gemma model and tokenizer
10
  @st.cache_resource
11
  def load_model():
12
- tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b-it")
13
  model = AutoModelForCausalLM.from_pretrained(
14
- "google/gemma-7b-it",
15
  torch_dtype=torch.float16,
16
  device_map="auto"
17
  )
@@ -29,20 +29,22 @@ def extract_text_from_pdf(file):
29
  return text.strip()
30
 
31
  # UI Layout
32
- query = st.text_input("Ask a question or type a query:")
33
 
34
- uploaded_file = st.file_uploader("Or upload a PDF to analyze its content:", type=["pdf"])
35
 
36
  context = ""
37
  if uploaded_file:
38
  context = extract_text_from_pdf(uploaded_file)
39
- st.text_area("Extracted Content", context, height=200)
40
 
41
  if st.button("Generate Answer"):
42
- with st.spinner("Generating with Gemma..."):
43
  prompt = query
44
  if context:
45
- prompt = f"Context:\n{context}\n\nQuestion: {query}"
 
 
46
  output = textgen(prompt)[0]["generated_text"]
47
  st.success("Answer:")
48
- st.write(output)
 
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
  )
 
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 = ""
37
  if uploaded_file:
38
  context = extract_text_from_pdf(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())