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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: apache-2.0
|
| 3 |
---
|
| 4 |
+
# Bella
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
import subprocess
|
| 10 |
+
|
| 11 |
+
# --- SETUP THE BRAIN ---
|
| 12 |
+
try:
|
| 13 |
+
import google.generativeai as genai
|
| 14 |
+
# PASTE YOUR KEY HERE
|
| 15 |
+
API_KEY = "API_KEY"
|
| 16 |
+
genai.configure(api_key=API_KEY)
|
| 17 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 18 |
+
HAS_BRAIN = True
|
| 19 |
+
except ImportError:
|
| 20 |
+
HAS_BRAIN = False
|
| 21 |
+
|
| 22 |
+
class BellaAI:
|
| 23 |
+
def speak(self, text):
|
| 24 |
+
print(f"Bella: {text}")
|
| 25 |
+
os.system(f'termux-tts-speak "{text}"')
|
| 26 |
+
|
| 27 |
+
def listen(self):
|
| 28 |
+
"""Uses Termux microphone to listen to you."""
|
| 29 |
+
print("Listening...")
|
| 30 |
+
try:
|
| 31 |
+
# This triggers the Android voice recognition popup
|
| 32 |
+
result = subprocess.check_output(["termux-speech-to-text"], stderr=subprocess.STDOUT)
|
| 33 |
+
return result.decode('utf-8').strip().lower()
|
| 34 |
+
except Exception:
|
| 35 |
+
return ""
|
| 36 |
+
|
| 37 |
+
def run(self):
|
| 38 |
+
self.speak("Bella is online and listening.")
|
| 39 |
+
|
| 40 |
+
while True:
|
| 41 |
+
# 1. Listen for your voice
|
| 42 |
+
user_speech = self.listen()
|
| 43 |
+
|
| 44 |
+
if not user_speech:
|
| 45 |
+
continue
|
| 46 |
+
|
| 47 |
+
print(f"You said: {user_speech}")
|
| 48 |
+
|
| 49 |
+
# 2. Check for exit commands
|
| 50 |
+
if any(word in user_speech for word in ['stop', 'exit', 'goodbye']):
|
| 51 |
+
self.speak("Goodbye Edward.")
|
| 52 |
+
break
|
| 53 |
+
|
| 54 |
+
# 3. Process with "Internet Brain"
|
| 55 |
+
if HAS_BRAIN and API_KEY != "API_KEY":
|
| 56 |
+
try:
|
| 57 |
+
response = model.generate_content(user_speech)
|
| 58 |
+
self.speak(response.text)
|
| 59 |
+
except Exception:
|
| 60 |
+
self.speak("I'm having trouble connecting to my brain.")
|
| 61 |
+
else:
|
| 62 |
+
self.speak("I heard you, but I need my API key to learn more.")
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
bella = BellaAI()
|
| 66 |
+
bella.run()
|
| 67 |
+
import os
|
| 68 |
+
import sys
|
| 69 |
+
|
| 70 |
+
# Try to import the Google AI library for the "Internet Brain"
|
| 71 |
+
try:
|
| 72 |
+
import google.generativeai as genai
|
| 73 |
+
HAS_GENAI = True
|
| 74 |
+
except ImportError:
|
| 75 |
+
HAS_GENAI = False
|
| 76 |
+
|
| 77 |
+
class BellaAI:
|
| 78 |
+
def __init__(self):
|
| 79 |
+
# --- CONFIGURATION ---
|
| 80 |
+
# Get a free key at: https://aistudio.google.com/
|
| 81 |
+
self.API_KEY = "YOUR_FREE_API_KEY_HERE"
|
| 82 |
+
|
| 83 |
+
if HAS_GENAI and self.API_KEY != "YOUR_FREE_API_KEY_HERE":
|
| 84 |
+
genai.configure(api_key=self.API_KEY)
|
| 85 |
+
self.model = genai.GenerativeModel('gemini-1.5-flash')
|
| 86 |
+
self.online_mode = True
|
| 87 |
+
else:
|
| 88 |
+
self.online_mode = False
|
| 89 |
+
|
| 90 |
+
# Fixed Dictionary Syntax
|
| 91 |
+
self.phrases = {
|
| 92 |
+
'my name is edward': {
|
| 93 |
+
'spanish': 'mi nombre es edward',
|
| 94 |
+
'french': 'mon nom est edward',
|
| 95 |
+
'german': 'mein name ist edward',
|
| 96 |
+
'italian': 'il mio nome è edward',
|
| 97 |
+
'mandarin': 'Wǒ de míngzì shì Edward'
|
| 98 |
+
},
|
| 99 |
+
'hello': {
|
| 100 |
+
'spanish': 'hola',
|
| 101 |
+
'french': 'bonjour',
|
| 102 |
+
'german': 'hallo',
|
| 103 |
+
'italian': 'ciao',
|
| 104 |
+
'mandarin': 'Nǐ hǎo'
|
| 105 |
+
},
|
| 106 |
+
'goodbye': {
|
| 107 |
+
'spanish': 'adiós',
|
| 108 |
+
'french': 'au revoir',
|
| 109 |
+
'german': 'auf wiedersehen',
|
| 110 |
+
'italian': 'arrivederci',
|
| 111 |
+
'mandarin': 'Zài jiàn'
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
print("✓ BellaAI initialized successfully")
|
| 116 |
+
if not self.online_mode:
|
| 117 |
+
print("⚠ Note: Running in Offline Mode. Add API Key for Internet Learning.")
|
| 118 |
+
|
| 119 |
+
def speak(self, text):
|
| 120 |
+
"""Uses Termux-API to speak. Ensure termux-api is installed."""
|
| 121 |
+
print(f"Bella: {text}")
|
| 122 |
+
# Clean text of quotes to avoid terminal errors
|
| 123 |
+
safe_text = text.replace('"', '')
|
| 124 |
+
# Direct command to Termux's internal TTS engine
|
| 125 |
+
os.system(f'termux-tts-speak "{safe_text}"')
|
| 126 |
+
|
| 127 |
+
def listen_text_input(self, prompt_message="Your input"):
|
| 128 |
+
try:
|
| 129 |
+
text = input(f"{prompt_message}: > ")
|
| 130 |
+
return text.strip()
|
| 131 |
+
except (EOFError, KeyboardInterrupt):
|
| 132 |
+
return "exit"
|
| 133 |
+
|
| 134 |
+
def run(self):
|
| 135 |
+
self.speak("System initialized. Bella is online.")
|
| 136 |
+
|
| 137 |
+
while True:
|
| 138 |
+
query = self.listen_text_input("You").lower()
|
| 139 |
+
|
| 140 |
+
if not query:
|
| 141 |
+
continue
|
| 142 |
+
|
| 143 |
+
if any(word in query for word in ['stop', 'exit', 'quit', 'bye']):
|
| 144 |
+
self.speak("Shutting down. Goodbye!")
|
| 145 |
+
break
|
| 146 |
+
|
| 147 |
+
# --- Logic: Translation ---
|
| 148 |
+
elif 'translate' in query:
|
| 149 |
+
self.speak("What phrase should I translate?")
|
| 150 |
+
phrase = self.listen_text_input("Phrase").lower()
|
| 151 |
+
self.speak("Which language? (spanish, french, german, italian, mandarin)")
|
| 152 |
+
lang = self.listen_text_input("Language").lower()
|
| 153 |
+
|
| 154 |
+
if phrase in self.phrases and lang in self.phrases[phrase]:
|
| 155 |
+
result = self.phrases[phrase][lang]
|
| 156 |
+
self.speak(f"In {lang}, that is: {result}")
|
| 157 |
+
else:
|
| 158 |
+
self.speak("I don't have that specific phrase saved.")
|
| 159 |
+
|
| 160 |
+
# --- Logic: Internet Learning (The Brain) ---
|
| 161 |
+
elif self.online_mode:
|
| 162 |
+
# If it's not a hardcoded command, Bella uses her "Internet Brain"
|
| 163 |
+
try:
|
| 164 |
+
response = self.model.generate_content(query)
|
| 165 |
+
self.speak(response.text)
|
| 166 |
+
except Exception as e:
|
| 167 |
+
self.speak("I had trouble reaching my internet brain.")
|
| 168 |
+
|
| 169 |
+
else:
|
| 170 |
+
# Default Offline Responses
|
| 171 |
+
if "hello" in query:
|
| 172 |
+
self.speak("Hello Edward! I am running offline right now.")
|
| 173 |
+
elif "who are you" in query:
|
| 174 |
+
self.speak("I am Bella, your personal AI assistant.")
|
| 175 |
+
else:
|
| 176 |
+
self.speak("I'm not sure how to do that offline. Please add my API key!")
|
| 177 |
+
|
| 178 |
+
if __name__ == "__main__":
|
| 179 |
+
# Check if Termux:API is accessible
|
| 180 |
+
if os.system('command -v termux-tts-speak > /dev/null 2>&1') != 0:
|
| 181 |
+
print("Error: 'termux-api' not found. Run 'pkg install termux-api' in Termux first.")
|
| 182 |
+
|
| 183 |
+
bella = BellaAI()
|
| 184 |
+
bella.run()
|