samim2024 commited on
Commit
404c584
·
verified ·
1 Parent(s): 9e73208

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from model import load_vectorstore, ask_question
3
+ import os
4
+
5
+ st.set_page_config(page_title="RAG with Mistral AI", layout="centered")
6
+
7
+ st.title("RAG Q&A App with Mistral AI")
8
+ st.write("Upload a document and ask questions about it.")
9
+
10
+ uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
11
+
12
+ if uploaded_file:
13
+ with open("document.pdf", "wb") as f:
14
+ f.write(uploaded_file.read())
15
+ st.success("File uploaded!")
16
+
17
+ if st.button("Index Document"):
18
+ with st.spinner("Processing..."):
19
+ load_vectorstore("document.pdf")
20
+ st.success("Vectorstore ready.")
21
+
22
+ query = st.text_input("Enter your question")
23
+ if st.button("Ask") and query:
24
+ with st.spinner("Generating answer..."):
25
+ answer = ask_question(query)
26
+ st.write("**Answer:**", answer)
27
+
28
+ # api.py
29
+ from fastapi import FastAPI, UploadFile, File, Form
30
+ from model import load_vectorstore, ask_question
31
+ import shutil
32
+
33
+ app = FastAPI()
34
+
35
+ @app.post("/upload")
36
+ async def upload(file: UploadFile = File(...)):
37
+ with open("document.pdf", "wb") as buffer:
38
+ shutil.copyfileobj(file.file, buffer)
39
+ load_vectorstore("document.pdf")
40
+ return {"message": "File indexed successfully."}
41
+
42
+ @app.post("/ask")
43
+ async def ask(question: str = Form(...)):
44
+ answer = ask_question(question)
45
+ return {"question": question, "answer": answer}
46
+