sancharikadebnath commited on
Commit
5699df4
β€’
1 Parent(s): 08ee655

Audio Feature

Browse files
Files changed (3) hide show
  1. .streamlit/config.toml +6 -0
  2. app.py +56 -33
  3. requirements.txt +3 -1
.streamlit/config.toml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ [theme]
2
+ primaryColor="#FF4B4B"
3
+ backgroundColor="#0E1117"
4
+ secondaryBackgroundColor="#262730"
5
+ textColor="#FAFAFA"
6
+ font="sans serif"
app.py CHANGED
@@ -1,32 +1,28 @@
 
 
1
  import streamlit as st
2
- from langchain.prompts import PromptTemplate
 
3
  import google.generativeai as genai
 
4
  from langchain_google_genai import ChatGoogleGenerativeAI
5
- import pyperclip
6
- import os
7
- from dotenv import load_dotenv
8
  load_dotenv()
9
- os.environ['GOOGLE_API_KEY']='AIzaSyAA59xkxMEUqffuaNMzrTxK6m5AJpY3tCw'
10
 
11
  def model():
12
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
13
  return ChatGoogleGenerativeAI(model="gemini-pro")
14
 
15
  def getLLamaresponse(input_text,no_words,blog_style):
16
-
17
  ## Prompt Template
18
-
19
  template="""
20
- Write a {tone} blog for {blog_style} job profile for a topic {input_text}
21
- within {no_words} words and ends with a conclusion. {info}.
22
  """
23
-
24
- prompt=PromptTemplate(input_variables=['tone',"blog_style","input_text",'no_words', "info"],
25
  template=template)
26
-
27
  ## Generate the ressponse from the LLama 2 model
28
- response=llm.invoke(prompt.format(tone=tone,blog_style=blog_style,input_text=input_text,no_words=no_words,info=info))
29
- print(response)
30
  return response.content
31
 
32
  def copy_text(answer, copy_button=False):
@@ -34,6 +30,31 @@ def copy_text(answer, copy_button=False):
34
  if copy_button:
35
  st.toast("Text copied to clipboard!", icon="πŸ“‹")
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  st.set_page_config(page_title="AI-Blogger",
39
  page_icon='πŸ€–',
@@ -42,39 +63,41 @@ st.set_page_config(page_title="AI-Blogger",
42
  st.header("AI Blogger πŸ€–", divider='rainbow')
43
 
44
 
45
- input_text=st.text_input("Enter the Blog Topic")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  with st.sidebar:
48
  st.title(' :blue[_AI Generated Blog_] πŸ€–')
49
-
50
  # Refactored from <https://github.com/a16z-infra/llama2-chatbot>
51
  st.subheader('Parameters')
52
-
53
  no_words = st.sidebar.slider('Maximum Characters', min_value=10, max_value=5000, value=1000, step=100)
54
- blog_style=st.selectbox('Writing the blog for',
55
- ('Researchers','Data Scientist','Common People'),index=0)
56
- tone=st.selectbox('Desired Tone',
57
- ('Informative','Casual','Persuasive', 'Formal', 'Humorous'),index=0)
58
-
59
  info = st.text_input('Specific Instruction')
60
  with st.spinner("Loading Model..."):
61
  llm= model()
62
-
63
-
64
- submit=st.button("Generate Blog")
65
 
66
-
67
- ## Final response
68
  if submit:
69
  with st.spinner("Generating Blog..."):
70
- answer = getLLamaresponse(input_text,no_words,blog_style)
71
  st.success('Blog Generated!', icon="βœ…")
72
  st.write(answer)
 
73
 
74
  ## Uncomment if using in system HF doesn't Support Copy to clipboard
75
  # st.button("πŸ“‹", on_click=copy_text(answer))
76
- st.markdown('''
77
-
78
-
79
- :orange[Run in your system to access Copy to Clipboard Feature]
80
- ''')
 
1
+ import os
2
+ import pyperclip
3
  import streamlit as st
4
+ from dotenv import load_dotenv
5
+ import speech_recognition as sr
6
  import google.generativeai as genai
7
+ from langchain.prompts import PromptTemplate
8
  from langchain_google_genai import ChatGoogleGenerativeAI
9
+
 
 
10
  load_dotenv()
 
11
 
12
  def model():
13
  genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
14
  return ChatGoogleGenerativeAI(model="gemini-pro")
15
 
16
  def getLLamaresponse(input_text,no_words,blog_style):
 
17
  ## Prompt Template
 
18
  template="""
19
+ Write a {tone} blog in {lang} as {blog_style} for a topic {input_text} within {no_words} words with references and video links also if possible and ends with a conclusion. {info}
 
20
  """
21
+ prompt=PromptTemplate(input_variables=['tone','lang',"blog_style","input_text",'no_words', "info"],
 
22
  template=template)
23
+ formated_prompt = prompt.format(tone=tone,lang=lang,blog_style=blog_style,input_text=input_text,no_words=no_words,info=info)
24
  ## Generate the ressponse from the LLama 2 model
25
+ response=llm.invoke(formated_prompt)
 
26
  return response.content
27
 
28
  def copy_text(answer, copy_button=False):
 
30
  if copy_button:
31
  st.toast("Text copied to clipboard!", icon="πŸ“‹")
32
 
33
+ def record_audio():
34
+ r = sr.Recognizer()
35
+ with st.spinner("Recording..."):
36
+ with sr.Microphone() as source:
37
+ r.adjust_for_ambient_noise(source) # Adjust for ambient noise
38
+ with st.spinner("Say Something..."):
39
+ audio = r.listen(source, timeout=5) # Set a timeout for listening
40
+ with st.spinner("Processing..."):
41
+ # Attempt speech recognition
42
+ try:
43
+ text = r.recognize_google(audio)
44
+ st.session_state['input_text'] = text
45
+ return text
46
+ except sr.UnknownValueError:
47
+ st.write("Sorry, I could not understand what you said. Please try again or write in text box.")
48
+ return ""
49
+ except sr.RequestError as e:
50
+ st.write(f"Could not request results; {e}")
51
+ return ""
52
+
53
+ def input_state(input_text):
54
+ if isinstance(input_text, str):
55
+ st.session_state['input_text'] = input_text # Update st.session_state with new text input
56
+
57
+
58
 
59
  st.set_page_config(page_title="AI-Blogger",
60
  page_icon='πŸ€–',
 
63
  st.header("AI Blogger πŸ€–", divider='rainbow')
64
 
65
 
66
+ col1,col2=st.columns([20,3])
67
+ with col1:
68
+ st.write('')
69
+ text = st.empty()
70
+ input_text = text.text_input("Enter the Blog Topic")
71
+
72
+
73
+ with col2:
74
+ st.write(':grey[.]')
75
+ recorder = st.button("πŸŽ™οΈ", help="Blog Topic by Voice")
76
+
77
+ if recorder:
78
+ recorded_text = record_audio()
79
+ if recorded_text:
80
+ input_text = text.text_input("Enter the Blog Topic",value=recorded_text)
81
 
82
  with st.sidebar:
83
  st.title(' :blue[_AI Generated Blog_] πŸ€–')
 
84
  # Refactored from <https://github.com/a16z-infra/llama2-chatbot>
85
  st.subheader('Parameters')
 
86
  no_words = st.sidebar.slider('Maximum Characters', min_value=10, max_value=5000, value=1000, step=100)
87
+ blog_style=st.selectbox('Writing the blog for', ('Researchers','Data Scientist','Common People'),index=0)
88
+ tone=st.selectbox('Desired Tone', ('Informative','Casual','Persuasive', 'Formal', 'Humorous'),index=0)
89
+ lang = st.text_input('Language', 'English')
 
 
90
  info = st.text_input('Specific Instruction')
91
  with st.spinner("Loading Model..."):
92
  llm= model()
 
 
 
93
 
94
+ submit=st.button("Generate Blog", on_click =lambda: input_state(input_text))
 
95
  if submit:
96
  with st.spinner("Generating Blog..."):
97
+ answer = getLLamaresponse(st.session_state['input_text'],no_words,blog_style)
98
  st.success('Blog Generated!', icon="βœ…")
99
  st.write(answer)
100
+ st.markdown(''':orange[Run in your system to access Copy to Clipboard Feature]''')
101
 
102
  ## Uncomment if using in system HF doesn't Support Copy to clipboard
103
  # st.button("πŸ“‹", on_click=copy_text(answer))
 
 
 
 
 
requirements.txt CHANGED
@@ -4,4 +4,6 @@ langchain
4
  streamlit
5
  pyperclip
6
  huggingface_hub
7
- python-dotenv
 
 
 
4
  streamlit
5
  pyperclip
6
  huggingface_hub
7
+ python-dotenv
8
+ SpeechRecognition
9
+ PyAudio