Spaces:
Sleeping
Sleeping
N.Achyuth Reddy
commited on
Commit
Β·
a6d394e
1
Parent(s):
a29f440
Update app.py
Browse files
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,
|
31 |
-
"transcribe",
|
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,
|
44 |
-
system_prompt,
|
45 |
-
max_new_tokens,
|
46 |
-
temperature,
|
47 |
Topp,
|
48 |
500,
|
49 |
-
Repetitionpenalty,
|
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)
|
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)
|
|
|
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)
|