Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- api.py +89 -0
- app.py +32 -0
- requirements.txt +10 -0
api.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from langchain.document_loaders import DirectoryLoader, PyPDFLoader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 5 |
+
from langchain.vectorstores import FAISS
|
| 6 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 7 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 8 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 9 |
+
from langchain.chains import create_retrieval_chain
|
| 10 |
+
import os
|
| 11 |
+
import uvicorn
|
| 12 |
+
|
| 13 |
+
# Initialize FastAPI App π
|
| 14 |
+
app = FastAPI(title="Vision Transformer Assistant", description="A FastAPI-powered AI assistant for deep learning.")
|
| 15 |
+
|
| 16 |
+
# Load Hugging Face Token π
|
| 17 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 18 |
+
|
| 19 |
+
if not HF_TOKEN:
|
| 20 |
+
raise ValueError("β οΈ HF_TOKEN is missing! Add it in Hugging Face Secrets.")
|
| 21 |
+
|
| 22 |
+
# Load Documents π
|
| 23 |
+
loader = DirectoryLoader("./data/", glob="*.pdf", loader_cls=PyPDFLoader)
|
| 24 |
+
docs = loader.load()
|
| 25 |
+
|
| 26 |
+
# Text Splitting π
|
| 27 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 28 |
+
texts = text_splitter.split_documents(docs)
|
| 29 |
+
|
| 30 |
+
# Vector Database π
|
| 31 |
+
db = FAISS.from_documents(documents=texts, embedding=HuggingFaceEmbeddings(model_name='BAAI/bge-base-en-v1.5'))
|
| 32 |
+
retriever = db.as_retriever()
|
| 33 |
+
|
| 34 |
+
# Load LLM π
|
| 35 |
+
repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 36 |
+
llm = HuggingFaceEndpoint(repo_id=repo_id, token=HF_TOKEN, task="text-generation")
|
| 37 |
+
|
| 38 |
+
# Prompt Template β¨
|
| 39 |
+
prompt_temp = ChatPromptTemplate.from_template("""
|
| 40 |
+
You are an AI assistant specializing in deep learning, specifically Vision Transformers.
|
| 41 |
+
|
| 42 |
+
<context>
|
| 43 |
+
{context}
|
| 44 |
+
<context>
|
| 45 |
+
|
| 46 |
+
### Instructions:
|
| 47 |
+
- Extract relevant information only from retrieved documents.
|
| 48 |
+
- Provide concise yet detailed responses.
|
| 49 |
+
- Use LaTeX for equations when necessary.
|
| 50 |
+
- Do not make up answers; respond with *'Information not available in retrieved documents.'* if needed.
|
| 51 |
+
""")
|
| 52 |
+
|
| 53 |
+
# Create Retrieval Chain β‘
|
| 54 |
+
document_chain = create_stuff_documents_chain(llm, prompt_temp)
|
| 55 |
+
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
| 56 |
+
|
| 57 |
+
def get_response(query: str) -> str:
|
| 58 |
+
"""
|
| 59 |
+
Get AI-generated response based on query.
|
| 60 |
+
"""
|
| 61 |
+
response = retrieval_chain.invoke({'input': query})
|
| 62 |
+
answer = response.get('answer', 'Error: No answer generated.')
|
| 63 |
+
|
| 64 |
+
# Debugging Logs
|
| 65 |
+
print(f"Query: {query} | Answer: {answer}")
|
| 66 |
+
|
| 67 |
+
return answer
|
| 68 |
+
|
| 69 |
+
@app.get("/")
|
| 70 |
+
def home():
|
| 71 |
+
return {"message": "Vision Transformer Assistant API is running π"}
|
| 72 |
+
|
| 73 |
+
@app.get("/query")
|
| 74 |
+
def get_answer(query: str):
|
| 75 |
+
"""
|
| 76 |
+
API endpoint to retrieve AI-generated responses.
|
| 77 |
+
"""
|
| 78 |
+
try:
|
| 79 |
+
answer = get_response(query)
|
| 80 |
+
return {"answer": answer}
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"Error: {e}")
|
| 83 |
+
return {"answer": "Error occurred while processing the request."}
|
| 84 |
+
|
| 85 |
+
# Run FastAPI
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 88 |
+
|
| 89 |
+
|
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# FastAPI Backend URL
|
| 5 |
+
FASTAPI_URL = "https://pranavreddy18-research.hf.space"
|
| 6 |
+
|
| 7 |
+
# Streamlit UI
|
| 8 |
+
st.set_page_config(page_title="Vision Transformer Assistant", page_icon="π€")
|
| 9 |
+
st.title("Vision Transformer Assistant π€")
|
| 10 |
+
st.markdown("Ask anything about deep learning and Vision Transformers!")
|
| 11 |
+
|
| 12 |
+
# User input
|
| 13 |
+
query = st.text_input("Enter your question:")
|
| 14 |
+
|
| 15 |
+
if st.button("Get Answer"):
|
| 16 |
+
if query:
|
| 17 |
+
with st.spinner("Fetching answer..."):
|
| 18 |
+
try:
|
| 19 |
+
response = requests.get(f"{FASTAPI_URL}/query", params={"query": query})
|
| 20 |
+
|
| 21 |
+
# Check if response is valid
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
answer = response.json().get("answer", "No answer found.")
|
| 24 |
+
st.success("β
Answer:")
|
| 25 |
+
st.write(answer)
|
| 26 |
+
else:
|
| 27 |
+
st.error(f"β οΈ Error fetching answer. Status Code: {response.status_code}")
|
| 28 |
+
st.write(response.text) # Debugging info
|
| 29 |
+
|
| 30 |
+
except requests.exceptions.RequestException as e:
|
| 31 |
+
st.error(f"β οΈ Failed to connect to backend: {e}")
|
| 32 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
streamlit
|
| 4 |
+
requests
|
| 5 |
+
langchain
|
| 6 |
+
langchain-core
|
| 7 |
+
langchain-huggingface
|
| 8 |
+
sentence-transformers
|
| 9 |
+
pypdf
|
| 10 |
+
faiss-cpu
|