File size: 1,621 Bytes
587fb3d
 
 
 
03836f6
587fb3d
03836f6
 
 
 
 
587fb3d
 
 
03836f6
 
 
 
587fb3d
03836f6
587fb3d
03836f6
 
587fb3d
 
 
 
 
 
 
 
d91a205
587fb3d
8752417
 
587fb3d
 
 
 
8752417
587fb3d
 
d91a205
03836f6
d91a205
587fb3d
03836f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import PyPDF2
import torch
import os

st.set_page_config(page_title="Perplexity-style Q&A (Mistral Auth)", layout="wide")
st.title("🧠 AI Study Assistant using Mistral 7B (Authenticated)")

# ✅ Load Hugging Face token from secrets
hf_token = os.getenv("HF_TOKEN")

@st.cache_resource
def load_model():
    tokenizer = AutoTokenizer.from_pretrained(
        "mistralai/Mistral-7B-Instruct-v0.1",
        token=hf_token
    )
    model = AutoModelForCausalLM.from_pretrained(
        "mistralai/Mistral-7B-Instruct-v0.1",
        torch_dtype=torch.float16,
        device_map="auto",
        token=hf_token
    )
    pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512)
    return pipe

textgen = load_model()

def extract_text_from_pdf(file):
    reader = PyPDF2.PdfReader(file)
    return "\n".join([p.extract_text() for p in reader.pages if p.extract_text()])

query = st.text_input("Ask a question or enter a topic:")
uploaded_file = st.file_uploader("Or upload a PDF to use as context:", type=["pdf"])

context = ""
if uploaded_file:
    context = extract_text_from_pdf(uploaded_file)
    st.text_area("📄 Extracted PDF Text", context, height=200)

if st.button("Generate Answer"):
    with st.spinner("Generating answer..."):
        prompt = f"[INST] Use the following context to answer the question:\n\n{context}\n\nQuestion: {query} [/INST]"
        result = textgen(prompt)[0]["generated_text"]
        st.success("Answer:")
        st.write(result.replace(prompt, "").strip())