-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelper.py
113 lines (80 loc) · 3.11 KB
/
helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import speech_recognition as sr
import google.generativeai as genai
from dotenv import load_dotenv
import os
from gtts import gTTS
import PIL.Image
import time
from markdown import Markdown
import streamlit as st
load_dotenv()
GOOGLE_API_KEY=os.getenv("GOOGLE_API_KEY")
os.environ["GOOGLE_API_KEY"]=GOOGLE_API_KEY
def voice_input():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Speak now...")
audio = r.listen(source)
try:
text=r.recognize_google(audio)
print("you said: ", text)
return text
except sr.UnknownValueError:
print("sorry, could not understand the audio")
except sr.RequestError as e:
print("could not request result from google speech recognition service: {0}".format(e))
def llm_model_audio(user_text):
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel(model_name='gemini-1.5-pro')
prompt1="""
You are a helpful assistant named 'ShauryaNova' developed by Ayush Shaurya Jha. You are
supposed to answer accurately and precisely to the user's
question.Now, the user query begins :
"""
content = f"{prompt1}\n{user_text}"
response = model.generate_content(content)
result = response.text
result_cleaned = result.replace('*', '')
return result_cleaned
def llm_model_image(user_text,path):
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel(model_name='gemini-1.5-pro')
prompt1="""
You are a helpful assistant named 'ShauryaNova' developed by Ayush Shaurya Jha. You are
supposed to answer accurately and precisely to the user's
question and image .Now, the user query begins :
"""
sample_file = PIL.Image.open(path)
prompt = f"{prompt1}\n{user_text}"
response = model.generate_content([prompt, sample_file])
result = response.text
return result
def llm_model_video(user_text, video_path):
genai.configure(api_key=GOOGLE_API_KEY)
model = genai.GenerativeModel(model_name='gemini-1.5-pro')
prompt1 = """
You are a helpful assistant named 'ShauryaNova' developed by Ayush Shaurya Jha .You are
supposed to answer accurately and precisely to the user's
question and video. Now, the user query begins:
"""
if video_path:
video_file_name = video_path
else:
raise ValueError("Video path cannot be None")
st.write(f"Uploading file: {video_file_name}")
video_file = genai.upload_file(path=video_file_name)
st.write(f"Completed upload: {video_file.uri}")
# Check whether the file is ready to be used.
while video_file.state.name == "PROCESSING":
st.write('.', end='')
time.sleep(10)
video_file = genai.get_file(video_file.name)
if video_file.state.name == "FAILED":
raise ValueError(video_file.state.name)
prompt = f"{prompt1}\n{user_text}" if user_text else prompt1
response = model.generate_content([video_file,prompt] ,request_options={"timeout": 600})
result = response.text
return result
def text_to_speech(text):
tts = gTTS(text=text, lang="en")
tts.save("speech.mp3")