N.Achyuth Reddy commited on
Commit
b453ff2
Β·
1 Parent(s): 950ee57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -49
app.py CHANGED
@@ -4,77 +4,57 @@ from st_audiorec import st_audiorec
4
  from gtts import gTTS
5
  import os
6
 
7
-
8
-
9
  # Constants
10
  TITLE = "AgriTure"
11
  DESCRIPTION = """
12
  ----
13
- 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.
14
  Hope this will be a Successful Project!!!
15
  ~Achyuth
16
  ----
17
  """
18
 
19
  # Initialize client
20
-
21
-
22
- with st.sidebar:
23
- system_promptSide = st.text_input("Optional system prompt:")
24
- temperatureSide = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.9, step=0.05)
25
- max_new_tokensSide = st.slider("Max new tokens", min_value=0.0, max_value=4096.0, value=4096.0, step=64.0)
26
- ToppSide = st.slider("Top-p (nucleus sampling)", min_value=0.0, max_value=1.0, value=0.6, step=0.05)
27
- RepetitionpenaltySide = st.slider("Repetition penalty", min_value=0.0, max_value=2.0, value=1.2, step=0.05)
28
-
29
  whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
30
 
 
 
 
 
 
31
 
 
32
  def transcribe(wav_path):
33
-
34
  return whisper_client.predict(
35
- wav_path, # str (filepath or URL to file) in 'inputs' Audio component
36
- "transcribe", # str in 'Task' Radio component
37
- api_name="/predict"
38
  )
39
 
40
  # Prediction function
41
- 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):
42
  with st.status("Starting client"):
43
  client = Client("https://huggingface-projects-llama-2-7b-chat.hf.space/")
44
  st.write("Requesting Audio Transcriber")
45
  with st.status("Requesting AgriTure v1"):
46
  st.write("Requesting API")
47
  response = client.predict(
48
- message, # str in 'Message' Textbox component
49
- system_prompt, # str in 'Optional system prompt' Textbox component
50
- max_new_tokens, # int | float (numeric value between 0 and 4096)
51
- temperature, # int | float (numeric value between 0.0 and 1.0)
52
- Topp,
53
- 500,
54
- Repetitionpenalty, # int | float (numeric value between 1.0 and 2.0)
55
- api_name="/chat"
56
  )
57
  st.write("Done")
58
  return response
59
-
60
-
61
- # Function to convert text to speech
62
- def text_to_speech(text, language='en', filename='output.mp3'):
63
- tts = gTTS(text=text, lang=language, slow=False)
64
- tts.save(filename)
65
- os.system(f'start {filename}')
66
- # Convert response to speech
67
- response=response
68
- text_to_speech(response)
69
-
70
  # Streamlit UI
71
  st.title(TITLE)
72
  st.write(DESCRIPTION)
73
 
74
-
75
-
76
-
77
-
78
  if "messages" not in st.session_state:
79
  st.session_state.messages = []
80
 
@@ -86,36 +66,50 @@ for message in st.session_state.messages:
86
  textinput = st.chat_input("Ask AgriTure anything...")
87
  wav_audio_data = st_audiorec()
88
 
89
- if wav_audio_data != None:
90
  with st.status("Transcribing audio..."):
91
  # save audio
92
  with open("audio.wav", "wb") as f:
93
  f.write(wav_audio_data)
94
  prompt = transcribe("audio.wav")
95
-
96
  st.write("Transcribed Given Audio βœ”")
97
-
98
- st.chat_message("human",avatar = "πŸ§‘β€πŸ’»").markdown(prompt)
99
  st.session_state.messages.append({"role": "human", "content": prompt})
100
 
101
  # transcribe audio
102
- response = predict(message= prompt)
103
 
104
- with st.chat_message("assistant", avatar='πŸ¦™'):
105
  st.markdown(response)
 
 
 
 
 
 
 
106
  # Add assistant response to chat history
107
  st.session_state.messages.append({"role": "assistant", "content": response})
108
 
109
  # React to user input
110
  if prompt := textinput:
111
  # Display user message in chat message container
112
- st.chat_message("human",avatar = "πŸ’¬: ").markdown(prompt)
113
  # Add user message to chat history
114
  st.session_state.messages.append({"role": "human", "content": prompt})
115
 
116
- response = predict(message=prompt)#, temperature= temperatureSide,max_new_tokens=max_new_tokensSide, Topp=ToppSide,Repetitionpenalty=RepetitionpenaltySide)
 
 
 
 
117
  # Display assistant response in chat message container
118
- with st.chat_message("assistant", avatar='πŸ¦™'):
119
  st.markdown(response)
 
 
 
 
120
  # Add assistant response to chat history
121
  st.session_state.messages.append({"role": "assistant", "content": 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 "AgriaTure". 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
  whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
19
 
20
+ # Function to convert text to speech using gTTS
21
+ def text_to_speech(text, lang='en'):
22
+ tts = gTTS(text=text, lang=lang, slow=False)
23
+ tts.save("response.mp3")
24
+ return "response.mp3"
25
 
26
+ # Function to transcribe audio
27
  def transcribe(wav_path):
 
28
  return whisper_client.predict(
29
+ wav_path,
30
+ "transcribe",
31
+ api_name="/predict"
32
  )
33
 
34
  # Prediction function
35
+ def predict(message, system_prompt='Your name is AgriaTure...', temperature=0.7, max_new_tokens=4096, Topp=0.5, Repetitionpenalty=1.2):
36
  with st.status("Starting client"):
37
  client = Client("https://huggingface-projects-llama-2-7b-chat.hf.space/")
38
  st.write("Requesting Audio Transcriber")
39
  with st.status("Requesting AgriTure v1"):
40
  st.write("Requesting API")
41
  response = client.predict(
42
+ message,
43
+ system_prompt,
44
+ max_new_tokens,
45
+ temperature,
46
+ Topp,
47
+ 500,
48
+ Repetitionpenalty,
49
+ api_name="/chat"
50
  )
51
  st.write("Done")
52
  return response
53
+
 
 
 
 
 
 
 
 
 
 
54
  # Streamlit UI
55
  st.title(TITLE)
56
  st.write(DESCRIPTION)
57
 
 
 
 
 
58
  if "messages" not in st.session_state:
59
  st.session_state.messages = []
60
 
 
66
  textinput = st.chat_input("Ask AgriTure anything...")
67
  wav_audio_data = st_audiorec()
68
 
69
+ if wav_audio_data is not None:
70
  with st.status("Transcribing audio..."):
71
  # save audio
72
  with open("audio.wav", "wb") as f:
73
  f.write(wav_audio_data)
74
  prompt = transcribe("audio.wav")
 
75
  st.write("Transcribed Given Audio βœ”")
76
+
77
+ st.chat_message("human", avatar="🌿").markdown(prompt)
78
  st.session_state.messages.append({"role": "human", "content": prompt})
79
 
80
  # transcribe audio
81
+ response = predict(message=prompt)
82
 
83
+ with st.chat_message("assistant", avatar='🌿'):
84
  st.markdown(response)
85
+
86
+ # Convert AI response to speech
87
+ speech_file = text_to_speech(response)
88
+
89
+ # Play the generated speech
90
+ st.audio(speech_file, format="audio/mp3")
91
+
92
  # Add assistant response to chat history
93
  st.session_state.messages.append({"role": "assistant", "content": response})
94
 
95
  # React to user input
96
  if prompt := textinput:
97
  # Display user message in chat message container
98
+ st.chat_message("human", avatar="🌿").markdown(prompt)
99
  # Add user message to chat history
100
  st.session_state.messages.append({"role": "human", "content": prompt})
101
 
102
+ response = predict(message=prompt)
103
+
104
+ # Convert AI response to speech
105
+ speech_file = text_to_speech(response)
106
+
107
  # Display assistant response in chat message container
108
+ with st.chat_message("assistant", avatar='🌿'):
109
  st.markdown(response)
110
+
111
+ # Play the generated speech
112
+ st.audio(speech_file, format="audio/mp3")
113
+
114
  # Add assistant response to chat history
115
  st.session_state.messages.append({"role": "assistant", "content": response})