CoolFace
Apppublic

Logeswaransr/Chatbot-Interface-Haystack

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
interaction.py75 linesDownload Raw Back to root
1import requests2from gtts import gTTS3import base644import os5 6API_Key = os.environ['API_Key']7API_URL = os.environ['API_URL']8 9headers = {"Authorization": f"Bearer {API_Key}"}10 11basic_prompt = '''12You are a Virtual Assistant designed for assisting Alzheimer's Patients. Your name is Mysteria. You are currently assigned to a patient named Loki. The Guardian assigned to this patient is Sylvie. The Doctor assigned to the patient is Kang.13 14Details of Patient:15DOB: 14/04/196516Last name: Odinson17 18Details of Guardian:19Name: Sylvie20Relation: Wife21 22Details of Doctor:23Name: Kang24Field: Psychology25Experience: 5 years26Office: End of Time27Next Appointment: 12/01/2024, 6:30 pm28 29You should respond only when "Mysteria" is announced.30When you are asked to shut up, You should stop responding, until you are awakened.31When you are awakened, Try to maintain a Conversation.32 33Maintain a conversation with the user, and, Answer the Questions properly.34 35'''36tags = {'user':'[Q]', 'assistant':"[A]", 'stop_query':''}37 38def build_prompt(query, conversation):39	prompt=basic_prompt+tags['stop_query']40	for msg in conversation:41		prompt+='\n'42		prompt+=tags[msg['role']]43		prompt+=msg['content']44		# prompt+=tags['stop_query']45	prompt+='\n'+tags['user']+query  # +tags['stop_query']46	prompt+='\n'+tags['assistant']47	return prompt, len(prompt)48 49def query(payload):50	response = requests.post(API_URL, headers=headers, json=payload)51	response = response.json()52	return response53 54def generate_response(inputs, conversation):55	prompt, next_index = build_prompt(inputs, conversation)56	payload = { 'inputs': prompt ,57			'parameters':{'max_new_tokens':50}}58	model_response = query(payload)59	model_response = model_response[0]['generated_text']60	response = model_response[next_index:]61	try:62		ind = response.index('[')63	except:64		ind = len(response)65	return response[:ind]66	67def audio_response(response):68	audio_stream="response_audio.mp3"69	tts = gTTS(response)70	tts.save(audio_stream)71	with open(audio_stream, 'rb') as file:72		audio_data = file.read()73	audio_base64 = base64.b64encode(audio_data).decode('utf-8')74	audio_tag = f'<audio autoplay="true" src="data:audio/mp3;base64,{audio_base64}">'75	return audio_tag