File size: 1,361 Bytes
404c584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 model import load_vectorstore, ask_question
import os

st.set_page_config(page_title="RAG with Mistral AI", layout="centered")

st.title("RAG Q&A App with Mistral AI")
st.write("Upload a document and ask questions about it.")

uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])

if uploaded_file:
    with open("document.pdf", "wb") as f:
        f.write(uploaded_file.read())
    st.success("File uploaded!")

    if st.button("Index Document"):
        with st.spinner("Processing..."):
            load_vectorstore("document.pdf")
            st.success("Vectorstore ready.")

query = st.text_input("Enter your question")
if st.button("Ask") and query:
    with st.spinner("Generating answer..."):
        answer = ask_question(query)
        st.write("**Answer:**", answer)

# api.py
from fastapi import FastAPI, UploadFile, File, Form
from model import load_vectorstore, ask_question
import shutil

app = FastAPI()

@app.post("/upload")
async def upload(file: UploadFile = File(...)):
    with open("document.pdf", "wb") as buffer:
        shutil.copyfileobj(file.file, buffer)
    load_vectorstore("document.pdf")
    return {"message": "File indexed successfully."}

@app.post("/ask")
async def ask(question: str = Form(...)):
    answer = ask_question(question)
    return {"question": question, "answer": answer}