Ath
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,10 +3,14 @@ import requests
|
|
3 |
import google.generativeai as genai
|
4 |
import gradio as gr
|
5 |
from tempfile import NamedTemporaryFile
|
|
|
6 |
|
7 |
# Configure your Google Generative AI API key
|
8 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
9 |
|
|
|
|
|
|
|
10 |
# Create the model
|
11 |
generation_config = {
|
12 |
"temperature": 1,
|
@@ -42,9 +46,27 @@ def chat_and_tts_text(user_input, history):
|
|
42 |
return history, f"Error: {str(e)}"
|
43 |
|
44 |
def convert_audio_to_text(audio_file):
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
def chat_and_tts_audio(audio_file, history):
|
50 |
try:
|
|
|
3 |
import google.generativeai as genai
|
4 |
import gradio as gr
|
5 |
from tempfile import NamedTemporaryFile
|
6 |
+
from google.cloud import speech
|
7 |
|
8 |
# Configure your Google Generative AI API key
|
9 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
10 |
|
11 |
+
# Set up Google Cloud credentials for Speech-to-Text
|
12 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path_to_your_google_cloud_credentials.json"
|
13 |
+
|
14 |
# Create the model
|
15 |
generation_config = {
|
16 |
"temperature": 1,
|
|
|
46 |
return history, f"Error: {str(e)}"
|
47 |
|
48 |
def convert_audio_to_text(audio_file):
|
49 |
+
try:
|
50 |
+
client = speech.SpeechClient()
|
51 |
+
|
52 |
+
with open(audio_file.name, "rb") as audio:
|
53 |
+
content = audio.read()
|
54 |
+
|
55 |
+
audio = speech.RecognitionAudio(content=content)
|
56 |
+
config = speech.RecognitionConfig(
|
57 |
+
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
|
58 |
+
sample_rate_hertz=16000,
|
59 |
+
language_code="en-US",
|
60 |
+
)
|
61 |
+
|
62 |
+
response = client.recognize(config=config, audio=audio)
|
63 |
+
|
64 |
+
# Assuming the audio contains only one speech segment
|
65 |
+
transcript = response.results[0].alternatives[0].transcript
|
66 |
+
return transcript
|
67 |
+
|
68 |
+
except Exception as e:
|
69 |
+
return f"Error in audio to text conversion: {str(e)}"
|
70 |
|
71 |
def chat_and_tts_audio(audio_file, history):
|
72 |
try:
|