Spaces:
Sleeping
Sleeping
Delete api.py
Browse files
api.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 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") # Ensure your HF_TOKEN is set in Hugging Face Secrets
|
| 18 |
-
|
| 19 |
-
# Load Documents π
|
| 20 |
-
loader = DirectoryLoader("./data/", glob="*.pdf", loader_cls=PyPDFLoader)
|
| 21 |
-
docs = loader.load()
|
| 22 |
-
|
| 23 |
-
# Text Splitting π
|
| 24 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 25 |
-
texts = text_splitter.split_documents(docs)
|
| 26 |
-
|
| 27 |
-
# Vector Database π
|
| 28 |
-
db = FAISS.from_documents(documents=texts, embedding=HuggingFaceEmbeddings(model_name='BAAI/bge-base-en-v1.5'))
|
| 29 |
-
retriever = db.as_retriever()
|
| 30 |
-
|
| 31 |
-
# Load LLM π
|
| 32 |
-
repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
|
| 33 |
-
llm = HuggingFaceEndpoint(repo_id=repo_id, token=HF_TOKEN, task="text-generation")
|
| 34 |
-
|
| 35 |
-
# Prompt Template β¨
|
| 36 |
-
prompt_temp = ChatPromptTemplate.from_template("""
|
| 37 |
-
You are an AI assistant specializing in deep learning, specifically Vision Transformers.
|
| 38 |
-
|
| 39 |
-
<context>
|
| 40 |
-
{context}
|
| 41 |
-
<context>
|
| 42 |
-
|
| 43 |
-
### Instructions:
|
| 44 |
-
- Extract relevant information only from retrieved documents.
|
| 45 |
-
- Provide concise yet detailed responses.
|
| 46 |
-
- Use LaTeX for equations when necessary.
|
| 47 |
-
- Do not make up answers; respond with *'Information not available in retrieved documents.'* if needed.
|
| 48 |
-
""")
|
| 49 |
-
|
| 50 |
-
# Create Retrieval Chain β‘
|
| 51 |
-
document_chain = create_stuff_documents_chain(llm, prompt_temp)
|
| 52 |
-
retrieval_chain = create_retrieval_chain(retriever, document_chain)
|
| 53 |
-
|
| 54 |
-
def get_response(query: str) -> str:
|
| 55 |
-
"""
|
| 56 |
-
Get AI-generated response based on query.
|
| 57 |
-
"""
|
| 58 |
-
response = retrieval_chain.invoke({'input': query})
|
| 59 |
-
return response['answer']
|
| 60 |
-
|
| 61 |
-
@app.get("/")
|
| 62 |
-
def home():
|
| 63 |
-
return {"message": "Vision Transformer Assistant API is running π"}
|
| 64 |
-
|
| 65 |
-
@app.get("/query")
|
| 66 |
-
def get_answer(query: str):
|
| 67 |
-
"""
|
| 68 |
-
API endpoint to retrieve AI-generated responses.
|
| 69 |
-
"""
|
| 70 |
-
answer = get_response(query)
|
| 71 |
-
return {"answer": answer}
|
| 72 |
-
|
| 73 |
-
# Run FastAPI on Hugging Face Spaces (public)
|
| 74 |
-
if __name__ == "__main__":
|
| 75 |
-
uvicorn.run(app, host="0.0.0.0", port=8080)
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|