Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline | |
import PyPDF2 | |
import torch | |
st.set_page_config(page_title="Perplexity-style Q&A (Mistral)", layout="wide") | |
st.title("🧠 Perplexity-style AI Study Assistant using Mistral 7B") | |
# Load Mistral model and tokenizer | |
def load_model(): | |
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1") | |
model = AutoModelForCausalLM.from_pretrained( | |
"mistralai/Mistral-7B-Instruct-v0.1", | |
torch_dtype=torch.float16, | |
device_map="auto" | |
) | |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=512) | |
return pipe | |
textgen = load_model() | |
# Extract text from uploaded PDF | |
def extract_text_from_pdf(file): | |
reader = PyPDF2.PdfReader(file) | |
text = "" | |
for page in reader.pages: | |
text += page.extract_text() + "\n" | |
return text.strip() | |
# UI Layout | |
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 with Mistral 7B..."): | |
prompt = query | |
if context: | |
prompt = f"[INST] Use the following context to answer the question:\n\n{context}\n\nQuestion: {query} [/INST]" | |
else: | |
prompt = f"[INST] {query} [/INST]" | |
output = textgen(prompt)[0]["generated_text"] | |
st.success("Answer:") | |
st.write(output.replace(prompt, "").strip()) |