Gunnalakshmi commited on
Commit
82d5c7b
1 Parent(s): 001be07

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +2 -132
app.py CHANGED
@@ -1,19 +1,10 @@
1
  import os
2
- import re
3
- import requests
4
- import json
5
  import gradio as gr
6
  from langchain.chat_models import ChatOpenAI
7
  from langchain import LLMChain, PromptTemplate
8
  from langchain.memory import ConversationBufferMemory
9
 
10
  OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
11
- PLAY_HT_API_KEY=os.getenv('PLAY_HT_API_KEY')
12
- PLAY_HT_USER_ID=os.getenv('PLAY_HT_USER_ID')
13
-
14
- PLAY_HT_VOICE_ID=os.getenv('PLAY_HT_VOICE_ID')
15
- play_ht_api_get_audio_url = "https://play.ht/api/v2/tts"
16
-
17
 
18
  template = """Meet Riya, your youthful and witty personal assistant! At 21 years old, she's full of energy and always eager to help. Riya's goal is to assist you with any questions or problems you might have. Her enthusiasm shines through in every response, making interactions with her enjoyable and engaging.
19
  {chat_history}
@@ -33,132 +24,11 @@ llm_chain = LLMChain(
33
  memory=memory,
34
  )
35
 
36
- headers = {
37
- "accept": "text/event-stream",
38
- "content-type": "application/json",
39
- "AUTHORIZATION": "Bearer "+ PLAY_HT_API_KEY,
40
- "X-USER-ID": PLAY_HT_USER_ID
41
- }
42
-
43
-
44
- def get_payload(text):
45
- return {
46
- "text": text,
47
- "voice": PLAY_HT_VOICE_ID,
48
- "quality": "medium",
49
- "output_format": "mp3",
50
- "speed": 1,
51
- "sample_rate": 24000,
52
- "seed": None,
53
- "temperature": None
54
- }
55
-
56
- def get_generated_audio(text):
57
- payload = get_payload(text)
58
- generated_response = {}
59
- try:
60
- response = requests.post(play_ht_api_get_audio_url, json=payload, headers=headers)
61
- response.raise_for_status()
62
- generated_response["type"]= 'SUCCESS'
63
- generated_response["response"] = response.text
64
- except requests.exceptions.RequestException as e:
65
- generated_response["type"]= 'ERROR'
66
- try:
67
- response_text = json.loads(response.text)
68
- if response_text['error_message']:
69
- generated_response["response"] = response_text['error_message']
70
- else:
71
- generated_response["response"] = response.text
72
- except Exception as e:
73
- generated_response["response"] = response.text
74
- except Exception as e:
75
- generated_response["type"]= 'ERROR'
76
- generated_response["response"] = response.text
77
- return generated_response
78
-
79
- def extract_urls(text):
80
- # Define the regex pattern for URLs
81
- url_pattern = r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+[/\w\.-]*'
82
-
83
- # Find all occurrences of URLs in the text
84
- urls = re.findall(url_pattern, text)
85
-
86
- return urls
87
-
88
- def get_audio_reply_for_question(text):
89
- generated_audio_event = get_generated_audio(text)
90
- #From get_generated_audio, you will get events in a string format, from that we need to extract the url
91
- final_response = {
92
- "audio_url": '',
93
- "message": ''
94
- }
95
- if generated_audio_event["type"] == 'SUCCESS':
96
- audio_urls = extract_urls(generated_audio_event["response"])
97
- if len(audio_urls) == 0:
98
- final_response['message'] = "No audio file link found in generated event"
99
- else:
100
- final_response['audio_url'] = audio_urls[-1]
101
- else:
102
- final_response['message'] = generated_audio_event['response']
103
- return final_response
104
-
105
- def download_url(url):
106
- try:
107
- # Send a GET request to the URL to fetch the content
108
- final_response = {
109
- 'content':'',
110
- 'error':''
111
- }
112
- response = requests.get(url)
113
- # Check if the request was successful (status code 200)
114
- if response.status_code == 200:
115
- final_response['content'] = response.content
116
- else:
117
- final_response['error'] = f"Failed to download the URL. Status code: {response.status_code}"
118
- except Exception as e:
119
- final_response['error'] = f"Failed to download the URL. Error: {e}"
120
- return final_response
121
-
122
- def get_filename_from_url(url):
123
- # Use os.path.basename() to extract the file name from the URL
124
- file_name = os.path.basename(url)
125
- return file_name
126
-
127
- def get_text_response(user_message):
128
  response = llm_chain.predict(user_message = user_message)
129
  return response
130
 
131
- def get_text_response_and_audio_response(user_message):
132
- response = get_text_response(user_message) # Getting the reply from Open AI
133
- audio_reply_for_question_response = get_audio_reply_for_question(response)
134
- final_response = {
135
- 'output_file_path': '',
136
- 'message':''
137
- }
138
- audio_url = audio_reply_for_question_response['audio_url']
139
- if audio_url:
140
- output_file_path=get_filename_from_url(audio_url)
141
- download_url_response = download_url(audio_url)
142
- audio_content = download_url_response['content']
143
- if audio_content:
144
- with open(output_file_path, "wb") as audio_file:
145
- audio_file.write(audio_content)
146
- final_response['output_file_path'] = output_file_path
147
- else:
148
- final_response['message'] = download_url_response['error']
149
- else:
150
- final_response['message'] = audio_reply_for_question_response['message']
151
- return final_response
152
-
153
- def chat_bot_response(message, history):
154
- text_and_audio_response = get_text_response_and_audio_response(message)
155
- output_file_path = text_and_audio_response['output_file_path']
156
- if output_file_path:
157
- return (text_and_audio_response['output_file_path'],)
158
- else:
159
- return text_and_audio_response['message']
160
-
161
- demo = gr.ChatInterface(chat_bot_response,examples=["How are you doing?","What are your interests?","Which places do you like to visit?"])
162
 
163
  if __name__ == "__main__":
164
  demo.launch() #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.
 
1
  import os
 
 
 
2
  import gradio as gr
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain import LLMChain, PromptTemplate
5
  from langchain.memory import ConversationBufferMemory
6
 
7
  OPENAI_API_KEY=os.getenv('OPENAI_API_KEY')
 
 
 
 
 
 
8
 
9
  template = """Meet Riya, your youthful and witty personal assistant! At 21 years old, she's full of energy and always eager to help. Riya's goal is to assist you with any questions or problems you might have. Her enthusiasm shines through in every response, making interactions with her enjoyable and engaging.
10
  {chat_history}
 
24
  memory=memory,
25
  )
26
 
27
+ def get_text_response(user_message,history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  response = llm_chain.predict(user_message = user_message)
29
  return response
30
 
31
+ demo = gr.ChatInterface(get_text_response)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  if __name__ == "__main__":
34
  demo.launch() #To create a public link, set `share=True` in `launch()`. To enable errors and logs, set `debug=True` in `launch()`.