File size: 16,166 Bytes
7f3430b
192447d
 
 
 
 
 
c2d26c1
192447d
c2d26c1
 
 
 
 
 
 
 
9916325
 
c2d26c1
 
 
 
192447d
c2d26c1
 
192447d
c2d26c1
 
 
b8d3256
 
c2d26c1
 
 
 
 
 
 
 
192447d
 
 
 
 
b8d3256
c2d26c1
 
 
 
 
 
 
 
 
 
 
 
 
 
192447d
 
 
 
 
b8d3256
c2d26c1
192447d
 
 
c2d26c1
 
 
b8d3256
192447d
 
 
 
 
 
 
b8d3256
192447d
 
 
 
 
 
 
9916325
c2d26c1
 
 
 
192447d
 
 
 
 
 
 
 
 
 
c2d26c1
 
 
 
192447d
 
 
 
 
 
 
 
c2d26c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192447d
 
 
 
 
c2d26c1
 
 
 
 
 
 
 
 
 
 
 
 
 
192447d
 
 
 
 
 
c2d26c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192447d
 
c2d26c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1009f8
 
 
 
 
b370650
c1009f8
 
 
 
 
 
8ff2c37
c1009f8
 
c2d26c1
 
c1009f8
b370650
c2d26c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
089a83f
c2d26c1
 
 
089a83f
b8d3256
c2d26c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a0c9f7
c2d26c1
 
 
 
 
 
192447d
c2d26c1
 
 
 
 
 
 
 
 
 
 
 
192447d
b8d3256
c2d26c1
 
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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
from langchain_community.vectorstores import Neo4jVector
from langchain_openai import OpenAIEmbeddings


#code for history
conversational_memory = ConversationBufferWindowMemory(
        memory_key='chat_history',
        k=10,
        return_messages=True
    )

# Setup Neo4j
graph = Neo4jGraph(
    url="neo4j+s://c62d0d35.databases.neo4j.io",
    username="neo4j",
    password="_x8f-_aAQvs2NB0x6s0ZHSh3W_y-HrENDbgStvsUCM0"
)


# directly show the graph resulting from the given Cypher query
default_cypher = "MATCH (s)-[r:!MENTIONS]->(t) RETURN s,r,t LIMIT 50"


vector_index = Neo4jVector.from_existing_graph(
    OpenAIEmbeddings(openai_api_key="sk-PV6RlpmTifrWo_olwL1IR69J9v2e5AKe-Xfxs_Yf9VT3BlbkFJm-UJQx5RNyGpok9MM_DYSTmayn7y-lKLSBqXecEoYA"),
    graph=graph,
    search_type="hybrid",
    node_label="Document",
    text_node_properties=["text"],
    embedding_node_property="embedding",
)

# 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"
    )

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 = 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(question: str):
    print(f"Search query: {question}")
    structured_data = structured_retriever(question)
    unstructured_data = [el.page_content for el in vector_index.similarity_search(question)]
    final_data = f"""Structured data:
{structured_data}
Unstructured data:
{"#Document ". join(unstructured_data)}
    """
    return final_data

# Setup for condensing the follow-up questions
_template = """Given the following conversation and a follow-up question, rephrase the follow-up question to be a standalone question,
in its original language.
Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:"""

CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)

def _format_chat_history(chat_history: list[tuple[str, str]]) -> list:
    buffer = []
    for human, ai in chat_history:
        buffer.append(HumanMessage(content=human))
        buffer.append(AIMessage(content=ai))
    return buffer

_search_query = RunnableBranch(
    # If input includes chat_history, we condense it with the follow-up question
    (
        RunnableLambda(lambda x: bool(x.get("chat_history"))).with_config(
            run_name="HasChatHistoryCheck"
        ),  # Condense follow-up question and chat into a standalone_question
        RunnablePassthrough.assign(
            chat_history=lambda x: _format_chat_history(x["chat_history"])
        )
        | CONDENSE_QUESTION_PROMPT
        | ChatOpenAI(temperature=0,openai_api_key="sk-PV6RlpmTifrWo_olwL1IR69J9v2e5AKe-Xfxs_Yf9VT3BlbkFJm-UJQx5RNyGpok9MM_DYSTmayn7y-lKLSBqXecEoYA")
        | StrOutputParser(),
    ),
    # Else, we have no chat history, so just pass through the question
    RunnableLambda(lambda x : x["question"]),
)


template = """I am a guide for Birmingham, Alabama. I can provide recommendations and insights about the city, including events and activities.
Ask your question directly, and I'll provide a precise and quick,short and crisp response in a conversational way without any Greet.
{context}
Question: {question}
Answer:"""


prompt = ChatPromptTemplate.from_template(template)

# Define the chain for Neo4j-based retrieval and response generation
chain_neo4j = (
    RunnableParallel(
        {
            "context": _search_query | retriever_neo4j,
            "question": RunnablePassthrough(),
        }
    )
    | prompt
    | 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



def handle_mode_selection(mode, chat_history, question):
    if mode == "Normal Chatbot":
        # Append the user's question to chat history first
        chat_history.append((question, ""))  # Placeholder for the bot's response
        
        # Stream the response and update chat history with each chunk
        for response_chunk in chat_with_bot(chat_history):
                chat_history[-1] = (question, response_chunk[-1][1])  # Update last entry with streamed response
                yield chat_history, "", None  # Stream each chunk to display in the chatbot
        yield chat_history, "", None  # Final yield to complete the response

    elif mode == "Voice to Voice Conversation":
        # Voice to Voice mode: Stream the response text and then convert it to audio
        response_text = get_response(question)  # Retrieve response text
        audio_path = generate_audio_elevenlabs(response_text)  # Convert response to audio
        yield [], "", audio_path  # Only output the audio response without updating chatbot history


# Function to add a user's message to the chat history and clear the input box
def add_message(history, message):
    if message.strip():
        history.append((message, ""))  # Add the user's message to the chat history only if it's not empty
    return history, ""  # Clear the input box
   
# Define function to generate a streaming response
def chat_with_bot(messages):
    user_message = messages[-1][0]  # Get the last user message (input)
    messages[-1] = (user_message, "")  # Prepare a placeholder for the bot's response
    
    response = get_response(user_message)  # Assume `get_response` is a generator function
    
    # Stream each character in the response and update the history progressively
    for character in response:
        messages[-1] = (user_message, messages[-1][1] + character)
        yield messages  # Stream each updated chunk
        time.sleep(0.05)  # Adjust delay as needed for real-time effect

    yield messages  # Final yield to complete the response


    
# Function to generate audio with Eleven Labs TTS from the last bot response
def generate_audio_from_last_response(history):
    # Get the most recent bot response from the chat history
    if history and len(history) > 0:
        recent_response = history[-1][1]  # The second item in the tuple is the bot response text
        if recent_response:
            return generate_audio_elevenlabs(recent_response)
    return None

# Define example prompts
examples = [
    ["What are some popular events in Birmingham?"],
    ["Who are the top players of the Crimson Tide?"],
    ["Where can I find a hamburger?"],
    ["What are some popular tourist attractions in Birmingham?"],
    ["What are some good clubs in Birmingham?"],
    ["Is there a farmer's market or craft fair in Birmingham, Alabama?"],
    ["Are there any special holiday events or parades in Birmingham, Alabama, during December?"],
    ["What are the best places to enjoy live music in Birmingham, Alabama?"]
    
]

# Function to insert the prompt into the textbox when clicked
def insert_prompt(current_text, prompt):
    return prompt[0] if prompt else current_text


# Define the ASR model with Whisper
model_id = 'openai/whisper-large-v3'
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, torch_dtype=torch_dtype).to(device)
processor = AutoProcessor.from_pretrained(model_id)

pipe_asr = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=15,
    batch_size=16,
    torch_dtype=torch_dtype,
    device=device,
    return_timestamps=True
)

# Define the function to reset the state after 10 seconds
def auto_reset_state():
    time.sleep(5)
    return None, ""  # Reset the state and clear input text


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

    # Ensure y is not empty and is at least 1-dimensional
    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

    # Ensure stream is also at least 1-dimensional before concatenation
    if stream is not None and len(stream) > 0:
        stream = np.concatenate([stream, y])
    else:
        stream = y

    # Process the audio data for transcription
    result = pipe_asr({"array": stream, "sampling_rate": sr}, return_timestamps=False)
    full_text = result.get("text", "")

    # Start a thread to reset the state after 10 seconds
    threading.Thread(target=auto_reset_state).start()

    return stream, full_text, full_text



# Define the function to clear the state and input text
def clear_transcription_state():
    return None, ""



with gr.Blocks(theme="rawrsor1/Everforest") as demo:
    chatbot = gr.Chatbot([], elem_id="RADAR", bubble_full_width=False)
    with gr.Row():
        with gr.Column():
            mode_selection = gr.Radio(
                choices=["Normal Chatbot", "Voice to Voice Conversation"],
                label="Mode Selection",
                value="Normal Chatbot"
            )
    with gr.Row():
        with gr.Column():
            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")

        with gr.Column():
            audio_output = gr.Audio(label="Audio", type="filepath", autoplay=True, interactive=False)
    
    with gr.Row():
        with gr.Column():
            get_response_btn = gr.Button("Get Response")
        with gr.Column():
            clear_state_btn = gr.Button("Clear State")
        with gr.Column():
            generate_audio_btn = gr.Button("Generate Audio")
        with gr.Column():
            clean_btn = gr.Button("Clean")

    with gr.Row():
        with gr.Column():
            gr.Markdown("<h1 style='color: red;'>Example Prompts</h1>", elem_id="Example-Prompts")
            gr.Examples(examples=examples, fn=insert_prompt, inputs=question_input, outputs=question_input, api_name="api_insert_example")

  
    # Define interactions for the Get Response button
    get_response_btn.click(
        fn=handle_mode_selection,
        inputs=[mode_selection, chatbot, question_input],
        outputs=[chatbot, question_input, audio_output],
        api_name="api_add_message_on_button_click"
        )

    
    

    question_input.submit(
        fn=handle_mode_selection,
        inputs=[mode_selection, chatbot, question_input],
        outputs=[chatbot, question_input, audio_output],
        api_name="api_add_message_on_enter"
    )

    
    submit_voice_btn.click(
        fn=handle_mode_selection,
        inputs=[mode_selection, 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"
    )    
    
    generate_audio_btn.click(
        fn=generate_audio_from_last_response,
        inputs=chatbot,
        outputs=audio_output,
        api_name="api_generate_text_to_audio"
    )
    
    clean_btn.click(
        fn=clear_fields,
        inputs=[],
        outputs=[chatbot, question_input, audio_output],
        api_name="api_clear_textbox"
    )

    # Clear state interaction
    clear_state_btn.click(
        fn=clear_transcription_state,
        outputs=[question_input, state],
        api_name="api_clean_state_transcription"
    )

# Launch the Gradio interface
demo.launch(show_error=True,share=True)