Spaces:
Sleeping
Sleeping
hasanriaz121
commited on
Commit
•
7c02716
1
Parent(s):
a9afbae
Added files
Browse files- docs.pkl +3 -0
- llm.py +26 -0
- main.py +73 -0
- requirements.txt +7 -0
- store.py +17 -0
docs.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ce3d8ea1903d18cef3d930e538c93c8640598be027bba6918a2c1016173aae47
|
3 |
+
size 1592911
|
llm.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_core.prompts import ChatPromptTemplate
|
2 |
+
from langchain_core.output_parsers import StrOutputParser
|
3 |
+
from langchain_core.runnables import RunnablePassthrough
|
4 |
+
from langchain_groq import ChatGroq
|
5 |
+
|
6 |
+
|
7 |
+
rag_template = """
|
8 |
+
Provide a summary from the context, which contains interpretations of Quranic Texts that highlight the importance of the topic mentioned in the question. Do not include the Quranic Texts themselves, but mention which Surah and verse.
|
9 |
+
|
10 |
+
Context:
|
11 |
+
{context}
|
12 |
+
|
13 |
+
Question:
|
14 |
+
{question}
|
15 |
+
"""
|
16 |
+
|
17 |
+
def create_chain(retreiver):
|
18 |
+
llm=ChatGroq(api_key="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"
|
19 |
+
,model="mixtral-8x7b-32768")
|
20 |
+
rag_prompt=ChatPromptTemplate.from_template(rag_template)
|
21 |
+
rag_chain=({"context":retreiver,"question":RunnablePassthrough()}
|
22 |
+
| rag_prompt
|
23 |
+
| llm
|
24 |
+
| StrOutputParser()
|
25 |
+
)
|
26 |
+
return rag_chain
|
main.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from contextlib import asynccontextmanager
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
|
7 |
+
from llm import create_chain
|
8 |
+
from store import create_store,get_retreiver,save_retreiver
|
9 |
+
|
10 |
+
# load_dotenv()
|
11 |
+
|
12 |
+
# @asynccontextmanager
|
13 |
+
# async def lifespan(app: FastAPI):
|
14 |
+
# # Load the ML model
|
15 |
+
# global chain,store
|
16 |
+
# # store=create_store()
|
17 |
+
# # chain=create_chain(store.as_retriever())
|
18 |
+
# yield
|
19 |
+
# # Clean up the ML models and release the resources
|
20 |
+
|
21 |
+
app = FastAPI()
|
22 |
+
|
23 |
+
@app.on_event("startup")
|
24 |
+
async def startup():
|
25 |
+
global chain,store
|
26 |
+
store=create_store()
|
27 |
+
chain=create_chain(store.as_retriever())
|
28 |
+
|
29 |
+
class Request(BaseModel):
|
30 |
+
prompt : str
|
31 |
+
|
32 |
+
class Response(BaseModel):
|
33 |
+
response : str
|
34 |
+
|
35 |
+
|
36 |
+
def greet(message,history):
|
37 |
+
return message
|
38 |
+
|
39 |
+
@app.post("/predict")
|
40 |
+
async def invoke_api(message,history="Abc"):
|
41 |
+
return chain.invoke(message)
|
42 |
+
|
43 |
+
@app.post("/test",response_model=Response)
|
44 |
+
async def predict_api(prompt:Request):
|
45 |
+
response = greet(Request.prompt)
|
46 |
+
return response
|
47 |
+
|
48 |
+
async def invoke(message,history):
|
49 |
+
return chain.invoke(message)
|
50 |
+
|
51 |
+
@app.get("/save_retreiver")
|
52 |
+
async def save_retreiver_api():
|
53 |
+
save_retreiver(store)
|
54 |
+
|
55 |
+
demo = gr.ChatInterface(
|
56 |
+
fn=invoke,
|
57 |
+
title="LLM App",
|
58 |
+
undo_btn="Delete Previous",
|
59 |
+
clear_btn="Clear",
|
60 |
+
)
|
61 |
+
|
62 |
+
|
63 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
64 |
+
|
65 |
+
|
66 |
+
# if __name__ == "__main__":
|
67 |
+
# # mounting at the root path
|
68 |
+
# uvicorn.run(
|
69 |
+
# app="main:app",
|
70 |
+
# host="localhost",#os.getenv("UVICORN_HOST"),
|
71 |
+
# port=8000#int(os.getenv("UVICORN_PORT"))
|
72 |
+
# )
|
73 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain_core
|
2 |
+
langchain_groq
|
3 |
+
langchain_community
|
4 |
+
gradio==4.8.0
|
5 |
+
fastapi
|
6 |
+
uvicorn
|
7 |
+
pydantic
|
store.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_community.vectorstores import Chroma
|
2 |
+
from langchain_community.embeddings import OllamaEmbeddings
|
3 |
+
from langchain_community.embeddings.huggingface import HuggingFaceEmbeddings
|
4 |
+
import pickle
|
5 |
+
|
6 |
+
|
7 |
+
def create_store():
|
8 |
+
docs=pickle.load(open("docs.pkl","rb"))
|
9 |
+
embeds=HuggingFaceEmbeddings(model_name="BAAI/bge-base-en-v1.5")
|
10 |
+
store=Chroma.from_documents(documents=docs,embedding=embeds)
|
11 |
+
return store
|
12 |
+
|
13 |
+
def get_retreiver(store:Chroma):
|
14 |
+
return store.as_retriever()
|
15 |
+
|
16 |
+
def save_retreiver(store:Chroma):
|
17 |
+
pickle.dump(store.as_retriever(),open("retreiver.pkl","wb"))
|