Here I want the llm to be used twice, first to generate the text query for web using ocr…
- User
Here I want the llm to be used twice, first to generate the text query for web using ocr data and my voice transcription and then second time to present the web results as it isin the original script :: import mss from PIL import Image import pytesseract import speech_recognition as sr import pyttsx3 import asyncio import aiohttp from bs4 import BeautifulSoup import ollama import keyboard import threading import time # For time.sleep()
Global variables for control is_listening = False speech_text = ""
Initialize the TTS engine globally tts_engine = pyttsx3.init() # Move this to the top
def configure_tts_voice(): global tts_engine voices = tts_engine.getProperty('voices')
List available voices (optional)
for index, voice in enumerate(voices): print(f"Voice {index}: {voice.name}, {voice.gender}, {voice.languages}")
Change voice (select male/female, adjust the index as per your preference)
tts_engine.setProperty('voice', voices[1].id) # Change index to select a different voice
Adjust speaking rate (lower is slower and more human-like)
tts_engine.setProperty('rate', 160) # Default is 200, lowering makes it more human-like
Adjust volume (0.0 to 1.0)
tts_engine.setProperty('volume', 1.0)
OCR function (using Tesseract) def perform_ocr(image): try: print("Performing OCR on the captured screen...") text = pytesseract.image_to_string(image) print(f"OCR complete: {text[:500]}...") # Limiting to first 100 characters for brevity return text except Exception as e: print(f"OCR Error: {e}") return ""
Screen capture function def capture_screen(): print("Capturing screen...") with mss.mss() as sct: monitor = sct.monitors[0] screenshot = sct.grab(monitor) print("Screen captured successfully.") return Image.frombytes("RGB", screenshot.size, screenshot.bgra, "raw", "BGRX")
Modified Speech-to-Text function with timeout handling def transcribe_speech(): global is_listening, speech_text recognizer = sr.Recognizer() with sr.Microphone() as source: print("Press 'F2' to start speaking, 'F3' to stop.") recognizer.adjust_for_ambient_noise(source, duration=1) # Adjust for noise for 1 second while not is_listening: time.sleep(0.1) print("Listening... Speak now.") try: # Listen for speech, but stop after a timeout audio = recognizer.listen(source, timeout=10, phrase_time_limit=15) # Timeout to avoid getting stuck print("Processing the audio with Google Speech Recognition...") text = recognizer.recognize_google(audio) speech_text = text print(f"Speech Transcribed: {text}") except sr.WaitTimeoutError: print("Listening timed out, no speech detected.") except sr.UnknownValueError: print("Speech recognition could not understand audio.") except sr.RequestError as e: print(f"Could not request results from speech recognition service; {e}")
Text-to-Speech function Text-to-Speech function with pause and stop control def speak_text(text): global tts_engine print("Speaking out the response...")
- Assistant