Spaces:
Runtime error
Runtime error
| 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 | |