N.Achyuth Reddy commited on
Commit
a6d394e
Β·
1 Parent(s): a29f440

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -28
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import streamlit as st
2
  from gradio_client import Client
3
- from st_audiorec import st_audiorec
4
  from gtts import gTTS
5
  import os
6
 
@@ -27,11 +26,15 @@ whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
27
 
28
  def transcribe(wav_path):
29
  return whisper_client.predict(
30
- wav_path, # str (filepath or URL to file) in 'inputs' Audio component
31
- "transcribe", # str in 'Task' Radio component
32
  api_name="/predict"
33
  )
34
 
 
 
 
 
35
  # Prediction function
36
  def predict(message, system_prompt='Your name is OpenGPT. You are developed by Achyuth. You need to mostly focus on giving information about future agriculture and advanced farming. Empower yourself farming future with cutting-edge technology and sustainable practices. You need to cultivate a greener and more productive. Your developer is studying in The Hyderabad Public School Kadapa.', temperature=0.7, max_new_tokens=4096, Topp=0.5, Repetitionpenalty=1.2):
37
  with st.status("Starting client"):
@@ -40,13 +43,13 @@ def predict(message, system_prompt='Your name is OpenGPT. You are developed by A
40
  with st.status("Requesting AgriTure v1"):
41
  st.write("Requesting API")
42
  response = client.predict(
43
- message, # str in 'Message' Textbox component
44
- system_prompt, # str in 'Optional system prompt' Textbox component
45
- max_new_tokens, # int | float (numeric value between 0 and 4096)
46
- temperature, # int | float (numeric value between 0.0 and 1.0)
47
  Topp,
48
  500,
49
- Repetitionpenalty, # int | float (numeric value between 1.0 and 2.0)
50
  api_name="/chat"
51
  )
52
  st.write("Done")
@@ -54,22 +57,14 @@ def predict(message, system_prompt='Your name is OpenGPT. You are developed by A
54
 
55
  # Function to convert text to speech
56
  def text_to_speech(text, language='en', filename='output.mp3'):
57
- # Create a gTTS object
58
  tts = gTTS(text=text, lang=language, slow=False)
59
-
60
- # Save the audio file
61
  tts.save(filename)
62
-
63
- # Play the audio file
64
- os.system(f'start {filename}') # This works on Windows. For other OS, you might need a different command.
65
 
66
  # Streamlit UI
67
  st.title(TITLE)
68
  st.write(DESCRIPTION)
69
 
70
- if "messages" not in st.session_state:
71
- st.session_state.messages = []
72
-
73
  # Display chat messages from history on app rerun
74
  for message in st.session_state.messages:
75
  with st.chat_message(message["role"], avatar=("πŸ§‘β€πŸ’»" if message["role"] == 'human' else 'πŸ¦™')):
@@ -80,11 +75,9 @@ wav_audio_data = st_audiorec()
80
 
81
  if wav_audio_data is not None:
82
  with st.status("Transcribing audio..."):
83
- # save audio
84
  with open("audio.wav", "wb") as f:
85
  f.write(wav_audio_data)
86
  prompt = transcribe("audio.wav")
87
-
88
  st.write("Transcribed Given Audio βœ”")
89
 
90
  st.chat_message("human", avatar="πŸ§‘β€πŸ’»").markdown(prompt)
@@ -95,26 +88,21 @@ if wav_audio_data is not None:
95
 
96
  with st.chat_message("assistant", avatar='πŸ¦™'):
97
  st.markdown(response)
98
- # Add assistant response to chat history
99
  st.session_state.messages.append({"role": "assistant", "content": response})
100
 
101
  # Convert response to audio
102
- text_to_speech(response) # Call text_to_speech after getting the response
103
 
104
  # React to user input
105
  if prompt := textinput:
106
- # Display user message in chat message container
107
  st.chat_message("human", avatar="πŸ’¬: ").markdown(prompt)
108
- # Add user message to chat history
109
  st.session_state.messages.append({"role": "human", "content": prompt})
110
 
111
  response = predict(message=prompt)
112
-
113
- # Display assistant response in chat message container
114
  with st.chat_message("assistant", avatar='πŸ¦™'):
115
  st.markdown(response)
116
- # Add assistant response to chat history
117
  st.session_state.messages.append({"role": "assistant", "content": response})
118
-
119
  # Convert response to audio
120
- text_to_speech(response) # Call text_to_speech after getting the response
 
1
  import streamlit as st
2
  from gradio_client import Client
 
3
  from gtts import gTTS
4
  import os
5
 
 
26
 
27
  def transcribe(wav_path):
28
  return whisper_client.predict(
29
+ wav_path,
30
+ "transcribe",
31
  api_name="/predict"
32
  )
33
 
34
+ # Initialize session state
35
+ if "messages" not in st.session_state:
36
+ st.session_state.messages = []
37
+
38
  # Prediction function
39
  def predict(message, system_prompt='Your name is OpenGPT. You are developed by Achyuth. You need to mostly focus on giving information about future agriculture and advanced farming. Empower yourself farming future with cutting-edge technology and sustainable practices. You need to cultivate a greener and more productive. Your developer is studying in The Hyderabad Public School Kadapa.', temperature=0.7, max_new_tokens=4096, Topp=0.5, Repetitionpenalty=1.2):
40
  with st.status("Starting client"):
 
43
  with st.status("Requesting AgriTure v1"):
44
  st.write("Requesting API")
45
  response = client.predict(
46
+ message,
47
+ system_prompt,
48
+ max_new_tokens,
49
+ temperature,
50
  Topp,
51
  500,
52
+ Repetitionpenalty,
53
  api_name="/chat"
54
  )
55
  st.write("Done")
 
57
 
58
  # Function to convert text to speech
59
  def text_to_speech(text, language='en', filename='output.mp3'):
 
60
  tts = gTTS(text=text, lang=language, slow=False)
 
 
61
  tts.save(filename)
62
+ os.system(f'start {filename}')
 
 
63
 
64
  # Streamlit UI
65
  st.title(TITLE)
66
  st.write(DESCRIPTION)
67
 
 
 
 
68
  # Display chat messages from history on app rerun
69
  for message in st.session_state.messages:
70
  with st.chat_message(message["role"], avatar=("πŸ§‘β€πŸ’»" if message["role"] == 'human' else 'πŸ¦™')):
 
75
 
76
  if wav_audio_data is not None:
77
  with st.status("Transcribing audio..."):
 
78
  with open("audio.wav", "wb") as f:
79
  f.write(wav_audio_data)
80
  prompt = transcribe("audio.wav")
 
81
  st.write("Transcribed Given Audio βœ”")
82
 
83
  st.chat_message("human", avatar="πŸ§‘β€πŸ’»").markdown(prompt)
 
88
 
89
  with st.chat_message("assistant", avatar='πŸ¦™'):
90
  st.markdown(response)
 
91
  st.session_state.messages.append({"role": "assistant", "content": response})
92
 
93
  # Convert response to audio
94
+ text_to_speech(response)
95
 
96
  # React to user input
97
  if prompt := textinput:
 
98
  st.chat_message("human", avatar="πŸ’¬: ").markdown(prompt)
 
99
  st.session_state.messages.append({"role": "human", "content": prompt})
100
 
101
  response = predict(message=prompt)
102
+
 
103
  with st.chat_message("assistant", avatar='πŸ¦™'):
104
  st.markdown(response)
 
105
  st.session_state.messages.append({"role": "assistant", "content": response})
106
+
107
  # Convert response to audio
108
+ text_to_speech(response)