Spaces:
Sleeping
Sleeping
Update interaction.py
Browse files- interaction.py +64 -3
interaction.py
CHANGED
|
@@ -1,12 +1,73 @@
|
|
| 1 |
-
import requests
|
| 2 |
from gtts import gTTS
|
| 3 |
import base64
|
| 4 |
import os
|
|
|
|
| 5 |
from haystack import Document
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
def init_doc_store():
|
| 8 |
docs = []
|
| 9 |
for file in files:
|
| 10 |
with open(path + '/' + file, 'r') as f:
|
| 11 |
content = f.read()
|
| 12 |
-
docs.append(Document(content=content, meta={'name':file}))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from gtts import gTTS
|
| 2 |
import base64
|
| 3 |
import os
|
| 4 |
+
|
| 5 |
from haystack import Document
|
| 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 init_doc_store(path, files):
|
| 13 |
docs = []
|
| 14 |
for file in files:
|
| 15 |
with open(path + '/' + file, 'r') as f:
|
| 16 |
content = f.read()
|
| 17 |
+
docs.append(Document(content=content, meta={'name':file}))
|
| 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)
|
| 68 |
+
tts.save(audio_stream)
|
| 69 |
+
with open(audio_stream, 'rb') as file:
|
| 70 |
+
audio_data = file.read()
|
| 71 |
+
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
| 72 |
+
audio_tag = f'<audio autoplay="true" src="data:audio/mp3;base64,{audio_base64}">'
|
| 73 |
+
return audio_tag
|