Spaces:
Runtime error
Runtime error
File size: 2,138 Bytes
b1ff431 |
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 |
from groq import Groq
import speech_recognition as sr
import requests
import os
import base64
import io
from dotenv import load_dotenv
load_dotenv()
client = Groq(
api_key= os.getenv('GROQ_API_KEY')
)
def speech_to_text(audio_binary, voice):
# Create a recognizer object
recognizer = sr.Recognizer()
# Use the WAV file with the speech recognition library
with sr.AudioFile(io.BytesIO(audio_binary)) as source:
# Record the audio data from the file
audio_data = recognizer.record(source)
try:
# Recognize the speech using Google Web Speech API
text = recognizer.recognize_google(audio_data, language = voice )
return text
except sr.UnknownValueError:
print("UnknownValueError: Speech recognition could not understand audio")
return None
except sr.RequestError as e:
print(f"RequestError: Could not request results from Google Web Speech API; {e}")
return None
except Exception as e:
print(f"An error occurred: {e}")
return None
def text_to_speech(text, voice=""):
return None
def process_message(conversation_history):
# Set the prompt for OpenAI Api
system_prompt = "Act like a personal assistant. You can respond to questions, translate sentences, summarize news, and give recommendations."
messages = [{"role": "system", "content": system_prompt}] + conversation_history
# Call the OpenAI Api to process our prompt
completion = client.chat.completions.create(
model="openai/gpt-oss-20b",
messages=messages,
temperature=0.55,
max_completion_tokens=4096,
top_p=1,
reasoning_effort="low",
stream=False,
stop=None,
tools=[{"type":"browser_search"},{"type":"code_interpreter"}],
tool_choice="auto"
)
# Parse the response to get the response message for our prompt
response_text = completion.choices[0].message.content
print(response_text)
return response_text
|