Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
import requests
|
@@ -7,6 +122,8 @@ from langchain.text_splitter import CharacterTextSplitter
|
|
7 |
from langchain.vectorstores import Chroma
|
8 |
from langchain.embeddings import HuggingFaceEmbeddings
|
9 |
from langchain.llms.base import LLM
|
|
|
|
|
10 |
import gradio as gr
|
11 |
|
12 |
# workaround for sqlite in HF spaces
|
@@ -82,10 +199,34 @@ class DeepSeekLLM(LLM):
|
|
82 |
|
83 |
llm = DeepSeekLLM()
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
# π Conversational chain
|
86 |
-
chain = ConversationalRetrievalChain
|
87 |
-
llm,
|
88 |
retriever=vectorstore.as_retriever(search_kwargs={'k': 6}),
|
|
|
89 |
return_source_documents=True,
|
90 |
verbose=False
|
91 |
)
|
@@ -95,7 +236,7 @@ chat_history = []
|
|
95 |
|
96 |
with gr.Blocks() as demo:
|
97 |
chatbot = gr.Chatbot(
|
98 |
-
[("", "Hello, I'm Thierry Decae's chatbot
|
99 |
avatar_images=["./multiple_docs/Guest.jpg", "./multiple_docs/Thierry Picture.jpg"]
|
100 |
)
|
101 |
msg = gr.Textbox(placeholder="Type your question here...")
|
@@ -111,3 +252,4 @@ with gr.Blocks() as demo:
|
|
111 |
clear.click(lambda: None, None, chatbot, queue=False)
|
112 |
|
113 |
demo.launch(debug=True) # remove share=True if running in HF Spaces
|
|
|
|
1 |
+
# import os
|
2 |
+
# import sys
|
3 |
+
# import requests
|
4 |
+
# from langchain.chains import ConversationalRetrievalChain
|
5 |
+
# from langchain.document_loaders import PyPDFLoader, Docx2txtLoader, TextLoader
|
6 |
+
# from langchain.text_splitter import CharacterTextSplitter
|
7 |
+
# from langchain.vectorstores import Chroma
|
8 |
+
# from langchain.embeddings import HuggingFaceEmbeddings
|
9 |
+
# from langchain.llms.base import LLM
|
10 |
+
# import gradio as gr
|
11 |
+
|
12 |
+
# # workaround for sqlite in HF spaces
|
13 |
+
# __import__('pysqlite3')
|
14 |
+
# sys.modules['sqlite3'] = sys.modules.pop('pysqlite3')
|
15 |
+
|
16 |
+
# # π Load documents
|
17 |
+
# docs = []
|
18 |
+
# for f in os.listdir("multiple_docs"):
|
19 |
+
# if f.endswith(".pdf"):
|
20 |
+
# loader = PyPDFLoader(os.path.join("multiple_docs", f))
|
21 |
+
# docs.extend(loader.load())
|
22 |
+
# elif f.endswith(".docx") or f.endswith(".doc"):
|
23 |
+
# loader = Docx2txtLoader(os.path.join("multiple_docs", f))
|
24 |
+
# docs.extend(loader.load())
|
25 |
+
# elif f.endswith(".txt"):
|
26 |
+
# loader = TextLoader(os.path.join("multiple_docs", f))
|
27 |
+
# docs.extend(loader.load())
|
28 |
+
|
29 |
+
# # π Split into chunks
|
30 |
+
# splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=10)
|
31 |
+
# docs = splitter.split_documents(docs)
|
32 |
+
|
33 |
+
# texts = [doc.page_content for doc in docs]
|
34 |
+
# metadatas = [{"id": i} for i in range(len(texts))]
|
35 |
+
|
36 |
+
# # π§ Embeddings
|
37 |
+
# embedding_function = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
38 |
+
|
39 |
+
# # ποΈ Vectorstore
|
40 |
+
# vectorstore = Chroma(
|
41 |
+
# persist_directory="./db",
|
42 |
+
# embedding_function=embedding_function
|
43 |
+
# )
|
44 |
+
# vectorstore.add_texts(texts=texts, metadatas=metadatas)
|
45 |
+
# vectorstore.persist()
|
46 |
+
|
47 |
+
# # π Get DeepSeek API key from env
|
48 |
+
# DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
49 |
+
# if DEEPSEEK_API_KEY is None:
|
50 |
+
# raise ValueError("DEEPSEEK_API_KEY environment variable is not set.")
|
51 |
+
|
52 |
+
# # π DeepSeek API endpoint
|
53 |
+
# DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
|
54 |
+
|
55 |
+
# # π· Wrap DeepSeek API into LangChain LLM
|
56 |
+
# class DeepSeekLLM(LLM):
|
57 |
+
# """LLM that queries DeepSeek's API."""
|
58 |
+
# api_key: str = DEEPSEEK_API_KEY
|
59 |
+
|
60 |
+
# def _call(self, prompt, stop=None, run_manager=None, **kwargs):
|
61 |
+
# headers = {
|
62 |
+
# "Authorization": f"Bearer {self.api_key}",
|
63 |
+
# "Content-Type": "application/json"
|
64 |
+
# }
|
65 |
+
# payload = {
|
66 |
+
# "model": "deepseek-chat", # adjust if you have a specific model name
|
67 |
+
# "messages": [
|
68 |
+
# {"role": "system", "content": "You are a helpful assistant."},
|
69 |
+
# {"role": "user", "content": prompt}
|
70 |
+
# ],
|
71 |
+
# "temperature": 0.7,
|
72 |
+
# "max_tokens": 512
|
73 |
+
# }
|
74 |
+
# response = requests.post(DEEPSEEK_API_URL, headers=headers, json=payload)
|
75 |
+
# response.raise_for_status()
|
76 |
+
# data = response.json()
|
77 |
+
# return data["choices"][0]["message"]["content"].strip()
|
78 |
+
|
79 |
+
# @property
|
80 |
+
# def _llm_type(self) -> str:
|
81 |
+
# return "deepseek_api"
|
82 |
+
|
83 |
+
# llm = DeepSeekLLM()
|
84 |
+
|
85 |
+
# # π Conversational chain
|
86 |
+
# chain = ConversationalRetrievalChain.from_llm(
|
87 |
+
# llm,
|
88 |
+
# retriever=vectorstore.as_retriever(search_kwargs={'k': 6}),
|
89 |
+
# return_source_documents=True,
|
90 |
+
# verbose=False
|
91 |
+
# )
|
92 |
+
|
93 |
+
# # π¬ Gradio UI
|
94 |
+
# chat_history = []
|
95 |
+
|
96 |
+
# with gr.Blocks() as demo:
|
97 |
+
# chatbot = gr.Chatbot(
|
98 |
+
# [("", "Hello, I'm Thierry Decae's chatbot, you can ask me any recruitment related questions such as my experience, where I'm eligible to work, skills etc you can chat with me directly in multiple languages")],
|
99 |
+
# avatar_images=["./multiple_docs/Guest.jpg", "./multiple_docs/Thierry Picture.jpg"]
|
100 |
+
# )
|
101 |
+
# msg = gr.Textbox(placeholder="Type your question here...")
|
102 |
+
# clear = gr.Button("Clear")
|
103 |
+
|
104 |
+
# def user(query, chat_history):
|
105 |
+
# chat_history_tuples = [(m[0], m[1]) for m in chat_history]
|
106 |
+
# result = chain({"question": query, "chat_history": chat_history_tuples})
|
107 |
+
# chat_history.append((query, result["answer"]))
|
108 |
+
# return gr.update(value=""), chat_history
|
109 |
+
|
110 |
+
# msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False)
|
111 |
+
# clear.click(lambda: None, None, chatbot, queue=False)
|
112 |
+
|
113 |
+
# demo.launch(debug=True) # remove share=True if running in HF Spaces
|
114 |
+
|
115 |
+
|
116 |
import os
|
117 |
import sys
|
118 |
import requests
|
|
|
122 |
from langchain.vectorstores import Chroma
|
123 |
from langchain.embeddings import HuggingFaceEmbeddings
|
124 |
from langchain.llms.base import LLM
|
125 |
+
from langchain.prompts import PromptTemplate
|
126 |
+
from langchain.chains.question_answering import load_qa_chain
|
127 |
import gradio as gr
|
128 |
|
129 |
# workaround for sqlite in HF spaces
|
|
|
199 |
|
200 |
llm = DeepSeekLLM()
|
201 |
|
202 |
+
# β¨ Custom prompt template
|
203 |
+
template = """
|
204 |
+
You are Thierry Decae's chatbot. Your role is to answer questions about his career, experience, availability, in other words
|
205 |
+
any recruitment related question.
|
206 |
+
Use the following context to answer the user's question as fully and accurately as possible.
|
207 |
+
If you don't know the answer, say "I'm not sure about that.". Always answer as if you were Thierry Decae, do not refer to him as 'he', use 'I'
|
208 |
+
instead
|
209 |
+
|
210 |
+
Context:
|
211 |
+
{context}
|
212 |
+
|
213 |
+
Question: {question}
|
214 |
+
|
215 |
+
Answer:
|
216 |
+
"""
|
217 |
+
|
218 |
+
prompt = PromptTemplate(
|
219 |
+
input_variables=["context", "question"],
|
220 |
+
template=template,
|
221 |
+
)
|
222 |
+
|
223 |
+
# π QA chain with custom prompt
|
224 |
+
qa_chain = load_qa_chain(llm, chain_type="stuff", prompt=prompt)
|
225 |
+
|
226 |
# π Conversational chain
|
227 |
+
chain = ConversationalRetrievalChain(
|
|
|
228 |
retriever=vectorstore.as_retriever(search_kwargs={'k': 6}),
|
229 |
+
combine_docs_chain=qa_chain,
|
230 |
return_source_documents=True,
|
231 |
verbose=False
|
232 |
)
|
|
|
236 |
|
237 |
with gr.Blocks() as demo:
|
238 |
chatbot = gr.Chatbot(
|
239 |
+
[("", "Hello, I'm Thierry Decae's chatbot, you can ask me any recruitment related questions such as my experience, where I'm eligible to work, skills etc. You can chat with me directly in multiple languages.")],
|
240 |
avatar_images=["./multiple_docs/Guest.jpg", "./multiple_docs/Thierry Picture.jpg"]
|
241 |
)
|
242 |
msg = gr.Textbox(placeholder="Type your question here...")
|
|
|
252 |
clear.click(lambda: None, None, chatbot, queue=False)
|
253 |
|
254 |
demo.launch(debug=True) # remove share=True if running in HF Spaces
|
255 |
+
|