Spaces:
Sleeping
Sleeping
N.Achyuth Reddy
commited on
Commit
Β·
a6532a3
1
Parent(s):
d06f4eb
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,53 @@ from st_audiorec import st_audiorec
|
|
4 |
from gtts import gTTS
|
5 |
import os
|
6 |
|
7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
# Function to convert text to speech
|
10 |
def text_to_speech(text, language='en', filename='output.mp3'):
|
@@ -17,7 +63,40 @@ def text_to_speech(text, language='en', filename='output.mp3'):
|
|
17 |
# Play the audio file
|
18 |
os.system(f'start {filename}') # This works on Windows. For other OS, you might need a different command.
|
19 |
|
20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# React to user input
|
23 |
if prompt := textinput:
|
@@ -26,14 +105,13 @@ if prompt := textinput:
|
|
26 |
# Add user message to chat history
|
27 |
st.session_state.messages.append({"role": "human", "content": prompt})
|
28 |
|
29 |
-
# Update the global response variable
|
30 |
response = predict(message=prompt)
|
31 |
-
|
32 |
# Display assistant response in chat message container
|
33 |
with st.chat_message("assistant", avatar='π¦'):
|
34 |
st.markdown(response)
|
35 |
# Add assistant response to chat history
|
36 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
37 |
-
|
38 |
# Convert response to audio
|
39 |
text_to_speech(response) # Call text_to_speech after getting the response
|
|
|
4 |
from gtts import gTTS
|
5 |
import os
|
6 |
|
7 |
+
# Constants
|
8 |
+
TITLE = "AgriTure"
|
9 |
+
DESCRIPTION = """
|
10 |
+
----
|
11 |
+
This Project demonstrates a model fine-tuned by Achyuth. This Model is named as "AgriTure". This Model helps the farmers and scientists to develop the art of agriculture and farming.
|
12 |
+
Hope this will be a Successful Project!!!
|
13 |
+
~Achyuth
|
14 |
+
----
|
15 |
+
"""
|
16 |
+
|
17 |
+
# Initialize client
|
18 |
+
with st.sidebar:
|
19 |
+
system_promptSide = st.text_input("Optional system prompt:")
|
20 |
+
temperatureSide = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.9, step=0.05)
|
21 |
+
max_new_tokensSide = st.slider("Max new tokens", min_value=0.0, max_value=4096.0, value=4096.0, step=64.0)
|
22 |
+
ToppSide = st.slider("Top-p (nucleus sampling)", min_value=0.0, max_value=1.0, value=0.6, step=0.05)
|
23 |
+
RepetitionpenaltySide = st.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.2, step=0.05)
|
24 |
+
|
25 |
+
whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
|
26 |
+
|
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"):
|
38 |
+
client = Client("https://huggingface-projects-llama-2-7b-chat.hf.space/")
|
39 |
+
st.write("Requesting Audio Transcriber")
|
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")
|
53 |
+
return response
|
54 |
|
55 |
# Function to convert text to speech
|
56 |
def text_to_speech(text, language='en', filename='output.mp3'):
|
|
|
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 'π¦')):
|
76 |
+
st.markdown(message["content"])
|
77 |
+
|
78 |
+
textinput = st.chat_input("Ask AgriTure anything...")
|
79 |
+
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)
|
91 |
+
st.session_state.messages.append({"role": "human", "content": prompt})
|
92 |
+
|
93 |
+
# transcribe audio
|
94 |
+
response = predict(message=prompt)
|
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 |
# React to user input
|
102 |
if prompt := textinput:
|
|
|
105 |
# Add user message to chat history
|
106 |
st.session_state.messages.append({"role": "human", "content": prompt})
|
107 |
|
|
|
108 |
response = predict(message=prompt)
|
109 |
+
|
110 |
# Display assistant response in chat message container
|
111 |
with st.chat_message("assistant", avatar='π¦'):
|
112 |
st.markdown(response)
|
113 |
# Add assistant response to chat history
|
114 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
115 |
+
|
116 |
# Convert response to audio
|
117 |
text_to_speech(response) # Call text_to_speech after getting the response
|