Spaces:
Sleeping
Sleeping
Logeswaransr
commited on
Update interaction.py
Browse files- interaction.py +9 -54
interaction.py
CHANGED
@@ -2,66 +2,21 @@ from gtts import gTTS
|
|
2 |
import base64
|
3 |
import os
|
4 |
|
5 |
-
from
|
6 |
-
from haystack.document_stores.in_memory import InMemoryDocumentStore
|
7 |
-
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
|
8 |
-
from haystack.components.builders import PromptBuilder
|
9 |
-
from haystack.components.generators.hugging_face_local import HuggingFaceLocalGenerator
|
10 |
-
from haystack.pipeline import Pipeline
|
11 |
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
document_store = InMemoryDocumentStore()
|
20 |
-
document_store.write_documents(docs)
|
21 |
-
return document_store
|
22 |
-
|
23 |
-
def define_components(document_store):
|
24 |
-
retriever = InMemoryBM25Retriever(document_store, top_k=3)
|
25 |
-
|
26 |
-
template = """
|
27 |
-
Given the following information, answer the question.
|
28 |
-
|
29 |
-
Context:
|
30 |
-
{% for document in documents %}
|
31 |
-
{{ document.content }}
|
32 |
-
{% endfor %}
|
33 |
-
|
34 |
-
Question: {{question}}
|
35 |
-
Answer:
|
36 |
-
"""
|
37 |
-
prompt_builder = PromptBuilder(template=template)
|
38 |
-
|
39 |
-
generator = HuggingFaceLocalGenerator(model="gpt2",
|
40 |
-
task="text-generation",
|
41 |
-
# device='cuda',
|
42 |
-
generation_kwargs={
|
43 |
-
"max_new_tokens": 100,
|
44 |
-
"temperature": 0.9,
|
45 |
-
})
|
46 |
-
generator.warm_up()
|
47 |
-
return retreiver, prompt_builder, generator
|
48 |
-
|
49 |
-
def define_pipeline(retreiver, prompt_builder, generator):
|
50 |
-
basic_rag_pipeline = Pipeline()
|
51 |
-
|
52 |
-
basic_rag_pipeline.add_component("retriever", retriever)
|
53 |
-
basic_rag_pipeline.add_component("prompt_builder", prompt_builder)
|
54 |
-
basic_rag_pipeline.add_component("llm", generator)
|
55 |
-
|
56 |
-
basic_rag_pipeline.connect("retriever", "prompt_builder.documents")
|
57 |
-
basic_rag_pipeline.connect("prompt_builder", "llm")
|
58 |
-
|
59 |
-
return basic_rag_pipeline
|
60 |
|
61 |
def generate_response(question, pipeline):
|
62 |
response = pipeline.run({'retriever':{"query":question}, 'prompt_builder':{'question':question}})
|
63 |
response = response['llm']['replies'][0]
|
64 |
return response
|
|
|
65 |
def audio_response(response):
|
66 |
audio_stream="response_audio.mp3"
|
67 |
tts = gTTS(response)
|
|
|
2 |
import base64
|
3 |
import os
|
4 |
|
5 |
+
from initialize import init_doc_store, define_components, define_pipeline
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
def init_pipeline():
|
8 |
+
path = 'RAG Files\'
|
9 |
+
files = os.listdir(path)
|
10 |
+
document_Store = init_doc_store(path, files)
|
11 |
+
retreiver, prompt_builder, generator = define_components(document_Store)
|
12 |
+
pipeline = define_pipeline(retreiver, prompt_builder, generator)
|
13 |
+
return pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def generate_response(question, pipeline):
|
16 |
response = pipeline.run({'retriever':{"query":question}, 'prompt_builder':{'question':question}})
|
17 |
response = response['llm']['replies'][0]
|
18 |
return response
|
19 |
+
|
20 |
def audio_response(response):
|
21 |
audio_stream="response_audio.mp3"
|
22 |
tts = gTTS(response)
|