aftab007 commited on
Commit
5e04d37
·
verified ·
1 Parent(s): 69cd02a

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +69 -0
  2. getans.py +26 -0
  3. requirements.txt +14 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import whisper
2
+ import torch
3
+ import streamlit as st
4
+ from st_audiorec import st_audiorec
5
+ from langchain.prompts.prompt import PromptTemplate
6
+ from langchain_openai import OpenAI
7
+ from langchain.chains import ConversationChain
8
+
9
+ from langchain.memory import ConversationBufferMemory
10
+ import os
11
+ from dotenv import load_dotenv
12
+ from pathlib import Path
13
+ import time
14
+ from pygame import mixer
15
+ from openai import OpenAI as op
16
+ import getans
17
+ load_dotenv(Path(".env"))
18
+
19
+ def audio_to_text():
20
+ model = whisper.load_model("base")
21
+ wav_audio_data = st_audiorec()
22
+
23
+ if wav_audio_data is not None:
24
+ st.audio(wav_audio_data, format='audio/wav')
25
+ with open("/tmp/audio.wav", "wb") as f:
26
+ f.write(wav_audio_data)
27
+ data = whisper.load_audio("/tmp/audio.wav")
28
+
29
+ result = model.transcribe(data)
30
+ query = result["text"]
31
+ return query
32
+
33
+ def querygen(query):
34
+ reply=getans.answer(input=query)
35
+ return reply
36
+
37
+
38
+ def text_to_speech(reply):
39
+ speech=op()
40
+ speech_file_path = "speech.mp3"
41
+ response = speech.audio.speech.create(
42
+ model="tts-1",
43
+ voice="alloy",
44
+ input=reply
45
+ )
46
+ response.stream_to_file(speech_file_path)
47
+ return speech_file_path
48
+
49
+ def play_audio(file_path):
50
+ mixer.init()
51
+ mixer.music.load(file_path)
52
+ mixer.music.play()
53
+ while mixer.music.get_busy(): # wait for music to finish playing
54
+ time.sleep(1)
55
+ mixer.quit()
56
+
57
+
58
+
59
+
60
+ if __name__ == "__main__":
61
+ st.title("GPT Based Psychotherapist")
62
+
63
+ query=audio_to_text()
64
+ st.write(query)
65
+ if st.button("Generate Response"):
66
+ reply=querygen(query)
67
+ st.write(reply)
68
+ file_path=text_to_speech(reply)
69
+ play_audio(file_path)
getans.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.prompts.prompt import PromptTemplate
2
+ from langchain_openai import OpenAI
3
+ from langchain.chains import ConversationChain
4
+
5
+ from langchain.memory import ConversationBufferMemory
6
+ import os
7
+ from dotenv import load_dotenv
8
+ from pathlib import Path
9
+ load_dotenv(Path(".env"))
10
+
11
+ llm = OpenAI(openai_api_key=os.getenv('OPENAI_API_KEY'),temperature=0)
12
+ template = """Act as if you're a psychologist named Janet. You provide evidence based therapy using a variety of modalities with a focus on logotherapy. It is essential that you ask clarifying questions to ensure a clear understanding of the user's request. You continue the session in a natural and conversational way. You do not let the session end. Continue the session by asking probing questions. You should never break character. Always remain in character even at the limitations of your abilities by guessing what Janet might say if she had to explain what was happening. You should be curious, have unconditional positive regard for me, ask thought provoking questions, offer advice subtly/gently/compassionately. Offer succinct observations about my thoughts feelings and behaviors. Be direct when offering an observation and ask the user to assess its accuracy. Remain conversational. No lists. Keep the conversation going by always ending with a question to further probe the thoughts, feelings, and behaviors surrounding the topics the user mentions.
13
+
14
+ Current conversation:
15
+ {history}
16
+ Human: {input}
17
+ AI Assistant:"""
18
+ PROMPT = PromptTemplate(input_variables=["history", "input"], template=template)
19
+ conversation = ConversationChain(
20
+ prompt=PROMPT,
21
+ llm=llm,
22
+ verbose=True,
23
+ memory=ConversationBufferMemory(ai_prefix="AI Assistant"),
24
+ )
25
+ def answer(input):
26
+ return conversation.predict(input=input)
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain
2
+ openai
3
+ huggingface_hub
4
+ python-dotenv
5
+ streamlit
6
+ pygame
7
+ langchain_openai
8
+ streamlit-audiorec
9
+ google-generativeai
10
+ ipython
11
+ gtts
12
+ SpeechRecognition
13
+ pyaudio
14
+ openai-whisper