File size: 7,859 Bytes
7f3430b
 
c71d159
193ef9a
8527f42
193ef9a
8527f42
 
193ef9a
8527f42
 
 
 
 
 
 
 
 
 
 
193ef9a
8527f42
 
 
7827ab9
8527f42
 
 
92b0167
8527f42
 
 
 
 
 
 
 
c71d159
 
 
 
92b0167
 
c71d159
 
 
 
 
 
 
 
 
 
 
165cb65
 
c71d159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8527f42
 
 
c71d159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8527f42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7702656
 
8527f42
7702656
 
165cb65
 
 
 
 
 
 
 
 
 
 
 
 
 
8527f42
 
 
 
 
 
 
 
 
 
 
7702656
 
8527f42
 
 
 
 
 
165cb65
8527f42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61ae7dd
8527f42
 
 
 
165cb65
8527f42
2eda2b6
8527f42
 
 
c689665
8527f42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28374c4
934e44a
8527f42
 
 
 
 
 
 
28374c4
934e44a
8527f42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import gradio as gr
import os
import logging
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
from langchain_community.graphs import Neo4jGraph
from typing import List, Tuple
from pydantic import BaseModel, Field
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.runnables import (
    RunnableBranch,
    RunnableLambda,
    RunnablePassthrough,
    RunnableParallel,
)
from langchain_core.prompts.prompt import PromptTemplate
import requests
import tempfile
from langchain.memory import ConversationBufferWindowMemory
import time
import logging
from langchain.chains import ConversationChain
import torch
import torchaudio
from transformers import pipeline, AutoModelForSpeechSeq2Seq, AutoProcessor
import numpy as np
import threading

# Setup conversational memory
conversational_memory = ConversationBufferWindowMemory(
    memory_key='chat_history',
    k=10,
    return_messages=True
)

# Setup Neo4j connection
graph = Neo4jGraph(
    url="neo4j+s://6457770f.databases.neo4j.io",
    username="neo4j",
    password="Z10duoPkKCtENuOukw3eIlvl0xJWKtrVSr-_hGX1LQ4"
)

# Define entity extraction and retrieval functions
class Entities(BaseModel):
    names: List[str] = Field(
        ..., description="All the person, organization, or business entities that appear in the text"
    )

entity_prompt = ChatPromptTemplate.from_messages([
    ("system", "You are extracting organization and person entities from the text."),
    ("human", "Use the given format to extract information from the following input: {question}"),
])

chat_model = ChatOpenAI(temperature=0, model_name="gpt-4o", api_key=os.environ['OPENAI_API_KEY'])
entity_chain = entity_prompt | chat_model.with_structured_output(Entities)

def remove_lucene_chars(input: str) -> str:
    return input.translate(str.maketrans({
        "\\": r"\\", "+": r"\+", "-": r"\-", "&": r"\&", "|": r"\|", "!": r"\!",
        "(": r"\(", ")": r"\)", "{": r"\{", "}": r"\}", "[": r"\[", "]": r"\]",
        "^": r"\^", "~": r"\~", "*": r"\*", "?": r"\?", ":": r"\:", '"': r'\"',
        ";": r"\;", " ": r"\ "
    }))

def generate_full_text_query(input: str) -> str:
    full_text_query = ""
    words = [el for el in remove_lucene_chars(input).split() if el]
    for word in words[:-1]:
        full_text_query += f" {word}~2 AND"
    full_text_query += f" {words[-1]}~2"
    return full_text_query.strip()

# Setup logging to a file to capture debug information
logging.basicConfig(filename='neo4j_retrieval.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

def structured_retriever(question: str) -> str:
    result = ""
    entities = entity_chain.invoke({"question": question})
    for entity in entities.names:
        response = graph.query(
            """CALL db.index.fulltext.queryNodes('entity', $query, {limit:2})
            YIELD node,score
            CALL {
              WITH node
              MATCH (node)-[r:!MENTIONS]->(neighbor)
              RETURN node.id + ' - ' + type(r) + ' -> ' + neighbor.id AS output
              UNION ALL
              WITH node
              MATCH (node)<-[r:!MENTIONS]-(neighbor)
              RETURN neighbor.id + ' - ' + type(r) + ' -> ' +  node.id AS output
            }
            RETURN output LIMIT 50
            """,
            {"query": generate_full_text_query(entity)},
        )
        result += "\n".join([el['output'] for el in response])
    return result

def retriever_neo4j(question: str):
    structured_data = structured_retriever(question)
    logging.debug(f"Structured data: {structured_data}")
    return structured_data

# Define the chain for Neo4j-based retrieval and response generation
chain_neo4j = (
    RunnableParallel(
        {
            "context": RunnableLambda(lambda x: retriever_neo4j(x["question"])),
            "question": RunnablePassthrough(),
        }
    )
    | ChatPromptTemplate.from_template("Answer: {context} Question: {question}")
    | chat_model
    | StrOutputParser()
)

# Define the function to get the response
def get_response(question):
    try:
        return chain_neo4j.invoke({"question": question})
    except Exception as e:
        return f"Error: {str(e)}"

# Define the function to clear input and output
def clear_fields():
    return [], "", None

# Function to generate audio with Eleven Labs TTS
def generate_audio_elevenlabs(text):
    XI_API_KEY = os.environ['ELEVENLABS_API']
    VOICE_ID = 'ehbJzYLQFpwbJmGkqbnW'
    tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
    headers = {
        "Accept": "application/json",
        "xi-api-key": XI_API_KEY
    }
    data = {
        "text": str(text),
        "model_id": "eleven_multilingual_v2",
        "voice_settings": {
            "stability": 1.0,
            "similarity_boost": 0.0,
            "style": 0.60,
            "use_speaker_boost": False
        }
    }
    response = requests.post(tts_url, headers=headers, json=data, stream=True)
    if response.ok:
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as f:
            for chunk in response.iter_content(chunk_size=1024):
                if chunk:
                    f.write(chunk)
            audio_path = f.name
        logging.debug(f"Audio saved to {audio_path}")
        return audio_path  # Return audio path for automatic playback
    else:
        logging.error(f"Error generating audio: {response.text}")
        return None

# Function to handle voice to voice conversation
def handle_voice_to_voice(chat_history, question):
    response = get_response(question)
    audio_path = generate_audio_elevenlabs(response)
    chat_history.append(("[Voice Input]", "[Voice Response]"))
    return chat_history, "", audio_path

# Function to transcribe audio input
def transcribe_function(stream, new_chunk):
    try:
        sr, y = new_chunk[0], new_chunk[1]
    except TypeError:
        print(f"Error chunk structure: {type(new_chunk)}, content: {new_chunk}")
        return stream, "", None

    if y is None or len(y) == 0:
        return stream, "", None

    y = y.astype(np.float32)
    max_abs_y = np.max(np.abs(y))
    if max_abs_y > 0:
        y = y / max_abs_y

    if stream is not None and len(stream) > 0:
        stream = np.concatenate([stream, y])
    else:
        stream = y

    result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
    full_text = result.get("text", "")

    threading.Thread(target=auto_reset_state).start()

    return stream, full_text, full_text

# Define the Gradio interface
with gr.Blocks(theme="rawrsor1/Everforest") as demo:
    chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
    mode_selection = gr.Radio(
        choices=["Normal Chatbot", "Voice to Voice Conversation"],
        label="Mode Selection",
        value="Normal Chatbot"
    )
    question_input = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")
    audio_input = gr.Audio(sources=["microphone"], streaming=True, type='numpy', every=0.1, label="Speak to Ask")
    submit_voice_btn = gr.Button("Submit Voice")
    audio_output = gr.Audio(label="Audio", type="filepath", autoplay=True, interactive=False)

    # Interactions for Submit Voice Button
    submit_voice_btn.click(
        fn=handle_voice_to_voice,
        inputs=[chatbot, question_input],
        outputs=[chatbot, question_input, audio_output],
        api_name="api_voice_to_voice_translation"
    )

    # Speech-to-Text functionality
    state = gr.State()
    audio_input.stream(
        transcribe_function,
        inputs=[state, audio_input],
        outputs=[state, question_input],
        api_name="api_voice_to_text"
    )

    demo.launch(show_error=True, share=True)