Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 7 |
-
st.title("
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
@st.cache_resource
|
| 11 |
def load_model():
|
| 12 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
-
"
|
| 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
|
| 33 |
|
| 34 |
-
uploaded_file = st.file_uploader("Or upload a PDF to
|
| 35 |
|
| 36 |
context = ""
|
| 37 |
if uploaded_file:
|
| 38 |
context = extract_text_from_pdf(uploaded_file)
|
| 39 |
-
st.text_area("Extracted
|
| 40 |
|
| 41 |
if st.button("Generate Answer"):
|
| 42 |
-
with st.spinner("Generating with
|
| 43 |
prompt = query
|
| 44 |
if context:
|
| 45 |
-
prompt = f"
|
|
|
|
|
|
|
| 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())
|