Spaces:
Sleeping
Sleeping
Maurizio Dipierro
commited on
Commit
·
38b9656
1
Parent(s):
6c94128
working gradio
Browse files- app.py +3 -13
- main.py +16 -12
- query_executor.py +11 -3
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
|
4 |
"""
|
5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
"""
|
@@ -25,19 +25,9 @@ def respond(
|
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
response =
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
yield response
|
41 |
|
42 |
|
43 |
"""
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from main import main
|
4 |
"""
|
5 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
"""
|
|
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
+
response = main(' '.join(message))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
return response
|
|
|
31 |
|
32 |
|
33 |
"""
|
main.py
CHANGED
@@ -1,14 +1,16 @@
|
|
1 |
import argparse
|
|
|
2 |
from document_handler import load_documents_from_disk, load_documents_from_sitemap, save_documents_to_disk
|
3 |
from vectorstore_handler import load_or_create_vectorstore, get_embeddings
|
4 |
from query_executor import execute_query
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
|
|
|
12 |
# Path to save the documents
|
13 |
sitemap_url = "https://www.originws.it/page-sitemap.xml"
|
14 |
docs_file_path = 'sitemap_docs.pkl'
|
@@ -17,22 +19,24 @@ def main():
|
|
17 |
docs = load_documents_from_disk(docs_file_path)
|
18 |
|
19 |
if docs is None:
|
20 |
-
|
21 |
# Load documents using SitemapLoader
|
22 |
docs = load_documents_from_sitemap(sitemap_url)
|
23 |
save_documents_to_disk(docs, docs_file_path)
|
24 |
-
|
25 |
else:
|
26 |
-
|
27 |
|
28 |
# Get embeddings and load/create the vectorstore
|
29 |
embeddings = get_embeddings()
|
30 |
vectorstore = load_or_create_vectorstore(docs, embeddings)
|
31 |
|
32 |
# Now that the vectorstore is ready, let's query it
|
33 |
-
question =
|
|
|
34 |
response = execute_query(question, vectorstore)
|
35 |
-
|
|
|
|
|
36 |
|
37 |
-
|
38 |
-
main()
|
|
|
1 |
import argparse
|
2 |
+
import logging
|
3 |
from document_handler import load_documents_from_disk, load_documents_from_sitemap, save_documents_to_disk
|
4 |
from vectorstore_handler import load_or_create_vectorstore, get_embeddings
|
5 |
from query_executor import execute_query
|
6 |
|
7 |
+
# Configure logging
|
8 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[
|
9 |
+
logging.FileHandler("query_executor.log"),
|
10 |
+
logging.StreamHandler()
|
11 |
+
])
|
12 |
|
13 |
+
def main(query):
|
14 |
# Path to save the documents
|
15 |
sitemap_url = "https://www.originws.it/page-sitemap.xml"
|
16 |
docs_file_path = 'sitemap_docs.pkl'
|
|
|
19 |
docs = load_documents_from_disk(docs_file_path)
|
20 |
|
21 |
if docs is None:
|
22 |
+
logging.info("Documents not found on disk, loading from sitemap...")
|
23 |
# Load documents using SitemapLoader
|
24 |
docs = load_documents_from_sitemap(sitemap_url)
|
25 |
save_documents_to_disk(docs, docs_file_path)
|
26 |
+
logging.info("Documents saved to disk.")
|
27 |
else:
|
28 |
+
logging.info("Documents loaded from disk.")
|
29 |
|
30 |
# Get embeddings and load/create the vectorstore
|
31 |
embeddings = get_embeddings()
|
32 |
vectorstore = load_or_create_vectorstore(docs, embeddings)
|
33 |
|
34 |
# Now that the vectorstore is ready, let's query it
|
35 |
+
question = query
|
36 |
+
logging.info(f"Executing query: {question}")
|
37 |
response = execute_query(question, vectorstore)
|
38 |
+
|
39 |
+
# Log the response
|
40 |
+
logging.info(f"Query response: {response}")
|
41 |
|
42 |
+
return response
|
|
query_executor.py
CHANGED
@@ -2,9 +2,12 @@ from langchain_openai import ChatOpenAI
|
|
2 |
from langchain_core.output_parsers import StrOutputParser
|
3 |
from langchain_core.prompts import ChatPromptTemplate
|
4 |
from langchain_core.runnables import RunnablePassthrough
|
|
|
|
|
5 |
|
6 |
RAG_TEMPLATE = """
|
7 |
-
You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.
|
|
|
8 |
|
9 |
<context>
|
10 |
{context}
|
@@ -25,8 +28,13 @@ def execute_query(question, vectorstore):
|
|
25 |
print(f"Found {len(docs)} relevant documents for the query.")
|
26 |
|
27 |
# Set up the LLM and prompt handling
|
28 |
-
llm = ChatOpenAI(model="gpt-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
30 |
# Define the RAG prompt template
|
31 |
rag_prompt = ChatPromptTemplate.from_template(RAG_TEMPLATE)
|
32 |
|
|
|
2 |
from langchain_core.output_parsers import StrOutputParser
|
3 |
from langchain_core.prompts import ChatPromptTemplate
|
4 |
from langchain_core.runnables import RunnablePassthrough
|
5 |
+
from langchain_anthropic import ChatAnthropic
|
6 |
+
|
7 |
|
8 |
RAG_TEMPLATE = """
|
9 |
+
You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise but friendly.
|
10 |
+
If the question is about yourself, answer you're the digital assistant coach of OriginWS.
|
11 |
|
12 |
<context>
|
13 |
{context}
|
|
|
28 |
print(f"Found {len(docs)} relevant documents for the query.")
|
29 |
|
30 |
# Set up the LLM and prompt handling
|
31 |
+
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
|
32 |
+
|
33 |
+
#llm = ChatAnthropic(model="claude-3-5-sonnet-20241022",temperature=0,max_tokens=1024,timeout=None,max_retries=2,
|
34 |
+
# api_key="...",
|
35 |
+
# base_url="...",
|
36 |
+
# other params...
|
37 |
+
#)
|
38 |
# Define the RAG prompt template
|
39 |
rag_prompt = ChatPromptTemplate.from_template(RAG_TEMPLATE)
|
40 |
|