Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,73 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
|
42 |
|
43 |
"""
|
44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
"""
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
)
|
59 |
-
|
60 |
-
)
|
61 |
-
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from langchain_core.vectorstores import InMemoryVectorStore
|
3 |
+
from langchain.chains import RetrievalQA
|
4 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
+
from langchain_groq import ChatGroq
|
6 |
+
from langchain_core.prompts import ChatPromptTemplate
|
7 |
+
from langchain.chains import create_retrieval_chain
|
8 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
9 |
|
10 |
"""
|
11 |
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
|
12 |
"""
|
|
|
13 |
|
14 |
+
model_name = "llama-3.3-70b-versatile"
|
15 |
+
embeddings = HuggingFaceEmbeddings(
|
16 |
+
model_name = "pkshatech/GLuCoSE-base-ja"
|
17 |
+
)
|
18 |
+
vector_store = InMemoryVectorStore.load(
|
19 |
+
"kaihatsu_vector_store", embeddings
|
20 |
+
)
|
21 |
+
retriever = vector_store.as_retriever(search_kwargs={"k": 4})
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
+
def fetch_response(groq_api_key, user_input):
|
25 |
+
chat = ChatGroq(
|
26 |
+
api_key = groq_api_key,
|
27 |
+
model_name = model_name
|
28 |
+
)
|
29 |
+
system_prompt = (
|
30 |
+
"あなたは便利なアシスタントです。"
|
31 |
+
"マニュアルの内容から回答してください。"
|
32 |
+
"\n\n"
|
33 |
+
"{context}"
|
34 |
+
)
|
35 |
|
36 |
+
prompt = ChatPromptTemplate.from_messages(
|
37 |
+
[
|
38 |
+
("system", system_prompt),
|
39 |
+
("human", "{input}"),
|
40 |
+
]
|
41 |
+
)
|
42 |
+
# ドキュメントのリストを渡せるchainを作成
|
43 |
+
question_answer_chain = create_stuff_documents_chain(chat, prompt)
|
44 |
+
# RetrieverとQAチェーンを組み合わせてRAGチェーンを作成
|
45 |
+
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
46 |
|
47 |
+
response = rag_chain.invoke({"input": user_input})
|
48 |
+
return [response["answer"], response["context"][0], response["context"][1]]
|
49 |
|
50 |
|
51 |
"""
|
52 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
53 |
"""
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown('''# 「スマート農業技術の開発・供給に関する事業」マスター \n
|
56 |
+
「スマート農業技術の開発・供給に関する事業」に関して、公募要領や審査要領を参考にRAGを使って回答します。
|
57 |
+
''')
|
58 |
+
with gr.Row():
|
59 |
+
api_key = gr.Textbox(label="Groq API key")
|
60 |
+
with gr.Row():
|
61 |
+
with gr.Column():
|
62 |
+
user_input = gr.Textbox(label="User Input")
|
63 |
+
submit = gr.Button("Submit")
|
64 |
+
answer = gr.Textbox(label="Answer")
|
65 |
+
with gr.Row():
|
66 |
+
with gr.Column():
|
67 |
+
source1 = gr.Textbox(label="回答ソース1")
|
68 |
+
with gr.Column():
|
69 |
+
source2 = gr.Textbox(label="回答ソース2")
|
70 |
+
submit.click(fetch_response, inputs=[api_key, user_input], outputs=[answer, source1, source2])
|
71 |
|
72 |
if __name__ == "__main__":
|
73 |
demo.launch()
|