Spaces:
Sleeping
Sleeping
styles and installs
Browse files- .gitignore +3 -1
- app.py +35 -3
- modules/langchain_init.py +25 -0
- modules/soup_extractor.py +11 -0
- requirements.txt +5 -1
- static/index.html +1 -1
- static/style.css +22 -0
.gitignore
CHANGED
@@ -33,4 +33,6 @@ instance/
|
|
33 |
dmypy.json
|
34 |
|
35 |
# Pyre type checker
|
36 |
-
.pyre/
|
|
|
|
|
|
33 |
dmypy.json
|
34 |
|
35 |
# Pyre type checker
|
36 |
+
.pyre/
|
37 |
+
|
38 |
+
*.ipynb
|
app.py
CHANGED
@@ -3,11 +3,39 @@ from os import getenv
|
|
3 |
from langchain_huggingface import HuggingFaceEmbeddings
|
4 |
from fastapi.responses import HTMLResponse, FileResponse
|
5 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
app = FastAPI()
|
8 |
MY_KEY = getenv("MY_KEY")
|
9 |
|
10 |
embeddings = HuggingFaceEmbeddings(model_name="jinaai/jina-embeddings-v2-small-en")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
|
13 |
|
@@ -23,14 +51,18 @@ async def chat(request: Request):
|
|
23 |
data = await request.json()
|
24 |
message = data.get("message")
|
25 |
# Process the message and generate a reply
|
26 |
-
|
|
|
27 |
return {"reply": reply}
|
28 |
|
29 |
@app.get("/embeddings")
|
30 |
def get_embeddings(input: str):
|
31 |
-
|
|
|
|
|
|
|
32 |
return {
|
33 |
-
"embeddings":
|
34 |
"test": "testtext"
|
35 |
}
|
36 |
|
|
|
3 |
from langchain_huggingface import HuggingFaceEmbeddings
|
4 |
from fastapi.responses import HTMLResponse, FileResponse
|
5 |
from fastapi.staticfiles import StaticFiles
|
6 |
+
from modules.langchain_init import get_llm
|
7 |
+
from modules.soup_extractor import bs4_extractor
|
8 |
+
from langchain_community.document_loaders import WebBaseLoader, RecursiveUrlLoader
|
9 |
+
from langchain_core.vectorstores import InMemoryVectorStore
|
10 |
+
from langchain import hub
|
11 |
|
12 |
app = FastAPI()
|
13 |
MY_KEY = getenv("MY_KEY")
|
14 |
|
15 |
embeddings = HuggingFaceEmbeddings(model_name="jinaai/jina-embeddings-v2-small-en")
|
16 |
+
llm = get_llm()
|
17 |
+
|
18 |
+
def create_loader(url:str):
|
19 |
+
return RecursiveUrlLoader(
|
20 |
+
# "https://book.cairo-lang.org/",
|
21 |
+
url,
|
22 |
+
extractor=bs4_extractor,
|
23 |
+
max_depth=2,
|
24 |
+
)
|
25 |
+
|
26 |
+
loader = {}
|
27 |
+
|
28 |
+
docs = []
|
29 |
+
|
30 |
+
my_vector_store = {}
|
31 |
+
|
32 |
+
prompt = hub.pull("rlm/rag-prompt")
|
33 |
+
|
34 |
+
def simple_rag(question, prompt):
|
35 |
+
retrieved_docs = my_vector_store.similarity_search(question)
|
36 |
+
docs_content = "\n\n".join(doc.page_content for doc in retrieved_docs)
|
37 |
+
prompt = prompt.invoke({"question": question, "context": docs_content})
|
38 |
+
return llm.invoke(prompt)
|
39 |
|
40 |
app.mount("/static", StaticFiles(directory="static", html=True), name="static")
|
41 |
|
|
|
51 |
data = await request.json()
|
52 |
message = data.get("message")
|
53 |
# Process the message and generate a reply
|
54 |
+
response = simple_rag(message, prompt)
|
55 |
+
reply = response.content
|
56 |
return {"reply": reply}
|
57 |
|
58 |
@app.get("/embeddings")
|
59 |
def get_embeddings(input: str):
|
60 |
+
loader = create_loader(input)
|
61 |
+
docs = loader.load()
|
62 |
+
my_vector_store = InMemoryVectorStore.from_documents(docs, embeddings)
|
63 |
+
|
64 |
return {
|
65 |
+
"embeddings": [],
|
66 |
"test": "testtext"
|
67 |
}
|
68 |
|
modules/langchain_init.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_openai import ChatOpenAI
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import os
|
4 |
+
# from langchain_cohere import CohereEmbeddings
|
5 |
+
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
openrouter_api_key=os.environ["OPENROUTER_API_KEY"]
|
10 |
+
# cohere_api_key=os.environ["COHERE_API_KEY"]
|
11 |
+
|
12 |
+
|
13 |
+
def get_llm(model_name: str = "openai/gpt-4o-mini"):
|
14 |
+
return ChatOpenAI(
|
15 |
+
model=model_name,
|
16 |
+
temperature=0.6,
|
17 |
+
openai_api_key=openrouter_api_key,
|
18 |
+
openai_api_base="https://openrouter.ai/api/v1"
|
19 |
+
)
|
20 |
+
|
21 |
+
# def get_embeddings(model_name: str = "embed-multilingual-light-v3.0"):
|
22 |
+
# return CohereEmbeddings(
|
23 |
+
# model=model_name,
|
24 |
+
# cohere_api_key=cohere_api_key,
|
25 |
+
# )
|
modules/soup_extractor.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from bs4 import BeautifulSoup, SoupStrainer
|
3 |
+
|
4 |
+
def_strainer = SoupStrainer(class_ = 'content')
|
5 |
+
|
6 |
+
def bs4_extractor(html: str, strainer: SoupStrainer = def_strainer) -> str:
|
7 |
+
'''
|
8 |
+
Extract text from html using BeautifulSoup
|
9 |
+
'''
|
10 |
+
soup = BeautifulSoup(html, "lxml", parse_only=strainer)
|
11 |
+
return re.sub(r"\n\n+", "\n\n", soup.text).strip()
|
requirements.txt
CHANGED
@@ -1,4 +1,8 @@
|
|
1 |
fastapi
|
2 |
uvicorn[standard]
|
|
|
3 |
langchain-huggingface
|
4 |
-
|
|
|
|
|
|
|
|
1 |
fastapi
|
2 |
uvicorn[standard]
|
3 |
+
langchain
|
4 |
langchain-huggingface
|
5 |
+
bs4
|
6 |
+
langchain_openai
|
7 |
+
langchain_community
|
8 |
+
lxml
|
static/index.html
CHANGED
@@ -35,7 +35,7 @@
|
|
35 |
<h2>Chat with the Model</h2>
|
36 |
<div id="chat-box"></div>
|
37 |
<form id="chat-form">
|
38 |
-
<
|
39 |
<button type="submit">Send</button>
|
40 |
</form>
|
41 |
</section>
|
|
|
35 |
<h2>Chat with the Model</h2>
|
36 |
<div id="chat-box"></div>
|
37 |
<form id="chat-form">
|
38 |
+
<textarea id="chat-input" placeholder="Type your message here..."></textarea>
|
39 |
<button type="submit">Send</button>
|
40 |
</form>
|
41 |
</section>
|
static/style.css
CHANGED
@@ -28,18 +28,40 @@ a {
|
|
28 |
form {
|
29 |
width: 30rem;
|
30 |
margin: 0 auto;
|
|
|
31 |
}
|
32 |
|
33 |
input {
|
34 |
width: 100%;
|
|
|
35 |
}
|
36 |
|
37 |
button {
|
38 |
cursor: pointer;
|
|
|
39 |
}
|
40 |
|
41 |
.text-gen-output {
|
42 |
min-height: 1.2rem;
|
43 |
margin: 1rem;
|
44 |
border: 0.5px solid grey;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
}
|
|
|
28 |
form {
|
29 |
width: 30rem;
|
30 |
margin: 0 auto;
|
31 |
+
border-radius: 3px;
|
32 |
}
|
33 |
|
34 |
input {
|
35 |
width: 100%;
|
36 |
+
border-radius: 3px;
|
37 |
}
|
38 |
|
39 |
button {
|
40 |
cursor: pointer;
|
41 |
+
border-radius: 3px;
|
42 |
}
|
43 |
|
44 |
.text-gen-output {
|
45 |
min-height: 1.2rem;
|
46 |
margin: 1rem;
|
47 |
border: 0.5px solid grey;
|
48 |
+
border-radius: 3px;
|
49 |
+
}
|
50 |
+
|
51 |
+
#chat-box {
|
52 |
+
width: 60%;
|
53 |
+
height: 40vh;
|
54 |
+
overflow-y: auto;
|
55 |
+
border: 1px solid #ccc;
|
56 |
+
padding: 1rem;
|
57 |
+
border-radius: 3px;
|
58 |
+
resize: none;
|
59 |
+
}
|
60 |
+
|
61 |
+
#chat-input {
|
62 |
+
width: 60%;
|
63 |
+
height: 3rem;
|
64 |
+
overflow-y: auto;
|
65 |
+
resize: none;
|
66 |
+
border-radius: 3px;
|
67 |
}
|