AchyuthGamer commited on
Commit
9030e5b
β€’
1 Parent(s): cab6a89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -29
app.py CHANGED
@@ -1,8 +1,11 @@
1
  import streamlit as st
2
  from gradio_client import Client
 
3
  from gtts import gTTS
4
  import os
5
 
 
 
6
  # Constants
7
  TITLE = "AgriTure"
8
  DESCRIPTION = """
@@ -14,6 +17,8 @@ Hope this will be a Successful Project!!!
14
  """
15
 
16
  # Initialize client
 
 
17
  with st.sidebar:
18
  system_promptSide = st.text_input("Optional system prompt:")
19
  temperatureSide = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.9, step=0.05)
@@ -25,28 +30,29 @@ whisper_client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
25
 
26
 
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 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):
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
@@ -56,12 +62,15 @@ def text_to_speech(text, language='en', filename='output.mp3'):
56
  tts = gTTS(text=text, lang=language, slow=False)
57
  tts.save(filename)
58
  os.system(f'start {filename}')
59
-
60
  # Streamlit UI
61
  st.title(TITLE)
62
  st.write(DESCRIPTION)
63
 
64
- # Initialize session state
 
 
 
65
  if "messages" not in st.session_state:
66
  st.session_state.messages = []
67
 
@@ -71,40 +80,41 @@ for message in st.session_state.messages:
71
  st.markdown(message["content"])
72
 
73
  textinput = st.chat_input("Ask AgriTure anything...")
 
74
 
75
- # Assume st_audiorec is a simplified audio recorder function
76
- wav_audio_data = st.audio_recorder()
77
-
78
- if wav_audio_data is not None:
79
  with st.status("Transcribing audio..."):
 
80
  with open("audio.wav", "wb") as f:
81
  f.write(wav_audio_data)
82
  prompt = transcribe("audio.wav")
 
83
  st.write("Transcribed Given Audio βœ”")
84
-
85
- st.chat_message("human", avatar="πŸ§‘β€πŸ’»").markdown(prompt)
86
  st.session_state.messages.append({"role": "human", "content": prompt})
87
 
88
  # transcribe audio
89
- response = predict(message=prompt)
90
 
91
  with st.chat_message("assistant", avatar='πŸ¦™'):
92
  st.markdown(response)
 
93
  st.session_state.messages.append({"role": "assistant", "content": response})
94
 
95
- # Convert response to audio
96
- text_to_speech(response)
97
-
98
  # React to user input
99
  if prompt := textinput:
100
- st.chat_message("human", avatar="πŸ’¬: ").markdown(prompt)
 
 
101
  st.session_state.messages.append({"role": "human", "content": prompt})
102
 
103
- response = predict(message=prompt)
104
-
105
  with st.chat_message("assistant", avatar='πŸ¦™'):
106
  st.markdown(response)
 
107
  st.session_state.messages.append({"role": "assistant", "content": response})
108
 
109
- # Convert response to audio
110
- text_to_speech(response)
 
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
 
7
+
8
+
9
  # Constants
10
  TITLE = "AgriTure"
11
  DESCRIPTION = """
 
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)
 
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
 
62
  tts = gTTS(text=text, lang=language, slow=False)
63
  tts.save(filename)
64
  os.system(f'start {filename}')
65
+
66
  # Streamlit UI
67
  st.title(TITLE)
68
  st.write(DESCRIPTION)
69
 
70
+
71
+
72
+
73
+
74
  if "messages" not in st.session_state:
75
  st.session_state.messages = []
76
 
 
80
  st.markdown(message["content"])
81
 
82
  textinput = st.chat_input("Ask AgriTure anything...")
83
+ wav_audio_data = st_audiorec()
84
 
85
+ if wav_audio_data != None:
 
 
 
86
  with st.status("Transcribing audio..."):
87
+ # save audio
88
  with open("audio.wav", "wb") as f:
89
  f.write(wav_audio_data)
90
  prompt = transcribe("audio.wav")
91
+
92
  st.write("Transcribed Given Audio βœ”")
93
+
94
+ st.chat_message("human",avatar = "πŸ§‘β€πŸ’»").markdown(prompt)
95
  st.session_state.messages.append({"role": "human", "content": prompt})
96
 
97
  # transcribe audio
98
+ response = predict(message= prompt)
99
 
100
  with st.chat_message("assistant", avatar='πŸ¦™'):
101
  st.markdown(response)
102
+ # Add assistant response to chat history
103
  st.session_state.messages.append({"role": "assistant", "content": response})
104
 
 
 
 
105
  # React to user input
106
  if prompt := textinput:
107
+ # Display user message in chat message container
108
+ st.chat_message("human",avatar = "πŸ’¬: ").markdown(prompt)
109
+ # Add user message to chat history
110
  st.session_state.messages.append({"role": "human", "content": prompt})
111
 
112
+ response = predict(message=prompt)#, temperature= temperatureSide,max_new_tokens=max_new_tokensSide, Topp=ToppSide,Repetitionpenalty=RepetitionpenaltySide)
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 speech
120
+ text_to_speech(response)