Spaces:
Sleeping
Sleeping
Commit ·
f52e95f
1
Parent(s): 2826905
Backend
Browse files- app.py +164 -15
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -1,6 +1,3 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
import faiss
|
| 5 |
import numpy as np
|
| 6 |
import pickle
|
|
@@ -8,13 +5,36 @@ import os
|
|
| 8 |
from optimum.onnxruntime import ORTModelForFeatureExtraction
|
| 9 |
from transformers import AutoTokenizer
|
| 10 |
from groq import Groq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
class MinimalEmbedding:
|
| 20 |
"""Lightweight embedding model using ONNX Runtime (no PyTorch)"""
|
|
@@ -100,6 +120,7 @@ Cite the sources with [Page X, Section Y]."""
|
|
| 100 |
|
| 101 |
embedding_model = MinimalEmbedding()
|
| 102 |
chunks, faiss_index = embed_and_cache_chunks([])
|
|
|
|
| 103 |
load_dotenv()
|
| 104 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 105 |
if not GROQ_API_KEY:
|
|
@@ -109,19 +130,147 @@ llm = Groq(
|
|
| 109 |
api_key=GROQ_API_KEY
|
| 110 |
)
|
| 111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
-
@app.get(
|
| 116 |
-
def
|
| 117 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
@app.post('/chat')
|
| 121 |
-
def runRag(body: Ques):
|
| 122 |
-
user_query = body.question
|
| 123 |
-
retrieved_chunks = retrieve_chunks(user_query, chunks, faiss_index)
|
| 124 |
-
final_prompt = build_prompt(user_query, retrieved_chunks)
|
| 125 |
-
response = llm.chat.completions.create(model="llama-3.1-8b-instant", messages=[{'role':"user","content":final_prompt}])
|
| 126 |
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import faiss
|
| 2 |
import numpy as np
|
| 3 |
import pickle
|
|
|
|
| 5 |
from optimum.onnxruntime import ORTModelForFeatureExtraction
|
| 6 |
from transformers import AutoTokenizer
|
| 7 |
from groq import Groq
|
| 8 |
+
from fastapi import FastAPI, Body
|
| 9 |
+
from fastapi.responses import JSONResponse
|
| 10 |
+
from pydantic import BaseModel
|
| 11 |
+
from pymongo import AsyncMongoClient
|
| 12 |
+
from bson import ObjectId
|
| 13 |
+
from dotenv import load_dotenv
|
| 14 |
+
import requests
|
| 15 |
+
|
| 16 |
+
load_dotenv()
|
| 17 |
|
| 18 |
app = FastAPI()
|
| 19 |
+
uri = os.getenv('MONGO_URI')
|
| 20 |
+
client = AsyncMongoClient(uri)
|
| 21 |
|
| 22 |
+
class Blogs(BaseModel):
|
| 23 |
+
timeToRead : str
|
| 24 |
+
blogDate : str
|
| 25 |
+
title : str
|
| 26 |
+
content : str
|
| 27 |
+
author : str
|
| 28 |
|
| 29 |
|
| 30 |
+
class Config:
|
| 31 |
+
json_encoders = {
|
| 32 |
+
ObjectId : str
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
class Ques(BaseModel):
|
| 36 |
+
question: str
|
| 37 |
+
|
| 38 |
|
| 39 |
class MinimalEmbedding:
|
| 40 |
"""Lightweight embedding model using ONNX Runtime (no PyTorch)"""
|
|
|
|
| 120 |
|
| 121 |
embedding_model = MinimalEmbedding()
|
| 122 |
chunks, faiss_index = embed_and_cache_chunks([])
|
| 123 |
+
|
| 124 |
load_dotenv()
|
| 125 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 126 |
if not GROQ_API_KEY:
|
|
|
|
| 130 |
api_key=GROQ_API_KEY
|
| 131 |
)
|
| 132 |
|
| 133 |
+
try:
|
| 134 |
+
database = client.get_database("MindfulNutrition")
|
| 135 |
+
blogs = database.get_collection("blogs")
|
| 136 |
+
except Exception as e:
|
| 137 |
+
raise Exception("Exception: ",e)
|
| 138 |
+
|
| 139 |
+
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
+
@app.get('/')
|
| 144 |
+
def index():
|
| 145 |
+
return JSONResponse(
|
| 146 |
+
status_code=200,
|
| 147 |
+
content={
|
| 148 |
+
"data":"server is running"
|
| 149 |
+
}
|
| 150 |
+
)
|
| 151 |
+
# return {"data": "server is running"}
|
| 152 |
|
| 153 |
+
# RAG
|
| 154 |
+
@app.post('/api/chat')
|
| 155 |
+
def chatResponse(body:Ques):
|
| 156 |
+
if body.question.strip() != '':
|
| 157 |
+
# LLM RAG code here
|
| 158 |
+
user_query = body.question
|
| 159 |
+
retrieved_chunks = retrieve_chunks(user_query, chunks, faiss_index)
|
| 160 |
+
final_prompt = build_prompt(user_query, retrieved_chunks)
|
| 161 |
+
response = llm.chat.completions.create(model="llama-3.1-8b-instant", messages=[{'role':"user","content":final_prompt}])
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
return JSONResponse(
|
| 165 |
+
status_code=200,
|
| 166 |
+
content={"success":True,"data":response.choices[0].message.content}
|
| 167 |
+
)
|
| 168 |
+
else:
|
| 169 |
+
return JSONResponse(
|
| 170 |
+
status_code=400,
|
| 171 |
+
content={"success":False,"data":"Question cannot be empty"}
|
| 172 |
+
)
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
# Blogs
|
| 176 |
+
@app.post('/api/createBlog')
|
| 177 |
+
async def getBlog(body:Blogs):
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
result = await blogs.insert_one(body.model_dump())
|
| 181 |
+
|
| 182 |
+
# return {"data":body.model_dump()}
|
| 183 |
+
if result.inserted_id:
|
| 184 |
+
return JSONResponse(
|
| 185 |
+
status_code=200,
|
| 186 |
+
content={
|
| 187 |
+
"data":"Blog created successfully!"
|
| 188 |
+
}
|
| 189 |
+
)
|
| 190 |
+
else:
|
| 191 |
+
return JSONResponse(
|
| 192 |
+
status_code=400,
|
| 193 |
+
content={
|
| 194 |
+
"data":"An error occured while creating the blog"
|
| 195 |
+
}
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
@app.get('/api/printBlogs')
|
| 199 |
+
async def get_all_blogs():
|
| 200 |
+
cursor = blogs.find()
|
| 201 |
+
blogsList = []
|
| 202 |
+
async for blog in cursor:
|
| 203 |
+
# print(type(blog))
|
| 204 |
+
blog['_id'] = str(blog['_id'])
|
| 205 |
+
# blog_dict = BlogsResponse(**blog)
|
| 206 |
+
blogsList.append(blog)
|
| 207 |
+
|
| 208 |
+
# return {'data':blogsList}
|
| 209 |
+
return JSONResponse(
|
| 210 |
+
status_code=200,
|
| 211 |
+
content={
|
| 212 |
+
"data":blogsList
|
| 213 |
+
}
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
@app.get('/api/Blogs/{id}')
|
| 217 |
+
async def get_specific_blog(id:str):
|
| 218 |
+
try:
|
| 219 |
+
blog = blogs.find_one({"_id": ObjectId(id)})
|
| 220 |
+
blog['_id'] = str(blog['_id'])
|
| 221 |
+
return JSONResponse(
|
| 222 |
+
status_code=200,
|
| 223 |
+
content={
|
| 224 |
+
"data": blog
|
| 225 |
+
}
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
except:
|
| 229 |
+
return JSONResponse(
|
| 230 |
+
status_code=200,
|
| 231 |
+
content={
|
| 232 |
+
"data": "blog not found"
|
| 233 |
+
}
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
@app.patch('/api/editBlog')
|
| 240 |
+
# Json
|
| 241 |
+
# {_id':'whtv','setter':{multiple or single updating jsons}}
|
| 242 |
+
async def editBlog(body:dict):
|
| 243 |
+
result = await blogs.update_one({'_id':ObjectId(body['_id'])},{'$set': body['setter']})
|
| 244 |
+
if result.matched_count == 0:
|
| 245 |
+
return JSONResponse(
|
| 246 |
+
status_code=400,
|
| 247 |
+
content={
|
| 248 |
+
"data": "Blog Not Found"
|
| 249 |
+
}
|
| 250 |
+
)
|
| 251 |
+
else:
|
| 252 |
+
return JSONResponse(
|
| 253 |
+
status_code=200,
|
| 254 |
+
content={
|
| 255 |
+
"data": "Blog updated successfully!"
|
| 256 |
+
}
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
@app.delete('/api/deleteBlog')
|
| 261 |
+
async def deleteBlog(body:dict):
|
| 262 |
+
result = await blogs.delete_one({'_id':ObjectId(body['_id'])})
|
| 263 |
+
if result.deleted_count>0:
|
| 264 |
+
return JSONResponse(
|
| 265 |
+
status_code=200,
|
| 266 |
+
content={
|
| 267 |
+
"data": "Blog deleted successfully!"
|
| 268 |
+
}
|
| 269 |
+
)
|
| 270 |
+
else:
|
| 271 |
+
return JSONResponse(
|
| 272 |
+
status_code=400,
|
| 273 |
+
content={
|
| 274 |
+
"data": "Blog was not found or was not deleted!"
|
| 275 |
+
}
|
| 276 |
+
)
|
requirements.txt
CHANGED
|
@@ -8,4 +8,6 @@ onnxruntime
|
|
| 8 |
onnx
|
| 9 |
optimum[onnxruntime]
|
| 10 |
groq
|
| 11 |
-
packaging
|
|
|
|
|
|
|
|
|
| 8 |
onnx
|
| 9 |
optimum[onnxruntime]
|
| 10 |
groq
|
| 11 |
+
packaging
|
| 12 |
+
pymongo
|
| 13 |
+
requests
|