Add files via upload
This commit is contained in:
15 files changed
+1347
No files matched your search
@@ -0,0 +1,48 @@
|
||||
# 🤖 Jarvis Mark X — Local Yapay Zeka Asistanı
|
||||
|
||||
Jarvis Mark X, **Python** ile sıfırdan geliştirilmiş, Jarvis’ten ilham alan **tamamen local** bir masaüstü yapay zeka asistanıdır.
|
||||
|
||||
Sesli komutlarla bilgisayar kontrolü sağlar ve intent → action mantığıyla çalışır.
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Özellikler
|
||||
- Uygulama açma
|
||||
- Mesaj gönderme (WhatsApp)
|
||||
- İnternet araması & hava durumu
|
||||
- Geçici & kalıcı hafıza
|
||||
- Tamamen local çalışma
|
||||
- İyi hafıza ile genel sohbet
|
||||
|
||||
## 🎥 Tutorial
|
||||
Bu proje bir **video anlatım serisinin** parçasıdır.
|
||||
Kod yapısını ve güvenli geliştirmeyi anlamak için videoyu izlemeniz önerilir.
|
||||
|
||||
---
|
||||
|
||||
## 🔐 Güvenlik Uyarısı
|
||||
- Program **%100 local çalışır**
|
||||
- Varsayılan olarak kimse erişemez
|
||||
- Jarvis sadece **senin yazdığın kodları** çalıştırır
|
||||
|
||||
⚠️ Bilinmeyen kodları yapıştırmayın ve sisteme tam yetki vermeyin.
|
||||
|
||||
---
|
||||
|
||||
## ⚠️ Kullanım Uyarısı
|
||||
- **Ticari kullanım yasaktır**
|
||||
- Satılamaz, pazarlanamaz
|
||||
- Kendi yapmış gibi sosyal medyada paylaşılamaz
|
||||
|
||||
Kişisel ve eğitim amaçlı kullanabilirsiniz.
|
||||
|
||||
---
|
||||
|
||||
## 📲 Takip & İletişim
|
||||
Instagram: https://www.instagram.com/fatihmakes/
|
||||
TikTok: https://www.tiktok.com/@fatihmakes
|
||||
YouTube:
|
||||
|
||||
Sorunlar için yazabilirsiniz.
|
||||
|
||||
⭐ Takip etmeyi unutmayın!
|
||||
@@ -0,0 +1,63 @@
|
||||
import time
|
||||
import pyautogui
|
||||
from tts import edge_speak
|
||||
|
||||
|
||||
def open_app(
|
||||
parameters: dict,
|
||||
response: str | None = None,
|
||||
player=None,
|
||||
session_memory=None
|
||||
) -> bool:
|
||||
"""
|
||||
Windows araması kullanarak bir uygulama açar.
|
||||
|
||||
parametreler:
|
||||
- app_name (str)
|
||||
|
||||
Bellek davranışı:
|
||||
- SADECE oturum belleğini kullanır
|
||||
- Uzun vadeli belleğe yazmaz
|
||||
"""
|
||||
|
||||
app_name = (parameters or {}).get("app_name", "").strip()
|
||||
|
||||
if not app_name and session_memory:
|
||||
app_name = session_memory.open_app or ""
|
||||
|
||||
if not app_name:
|
||||
msg = "Efendim, hangi uygulamanın açılacağını belirleyemedim."
|
||||
if player:
|
||||
player.write_log(msg)
|
||||
edge_speak(msg, player)
|
||||
return False
|
||||
|
||||
if response:
|
||||
if player:
|
||||
player.write_log(response)
|
||||
edge_speak(response, player)
|
||||
|
||||
try:
|
||||
pyautogui.PAUSE = 0.1
|
||||
|
||||
|
||||
pyautogui.press("win")
|
||||
time.sleep(0.3)
|
||||
|
||||
pyautogui.write(app_name, interval=0.03)
|
||||
time.sleep(0.2)
|
||||
|
||||
pyautogui.press("enter")
|
||||
time.sleep(0.6)
|
||||
|
||||
if session_memory:
|
||||
session_memory.set_open_app(app_name)
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
msg = f"Efendim, {app_name} uygulamasını açamadım."
|
||||
if player:
|
||||
player.write_log(f"{msg} ({e})")
|
||||
edge_speak(msg, player)
|
||||
return False
|
||||
@@ -0,0 +1,98 @@
|
||||
import time
|
||||
import pyautogui
|
||||
from tts import edge_speak
|
||||
import pyperclip
|
||||
|
||||
REQUIRED_PARAMS = ["receiver", "message_text", "platform"]
|
||||
|
||||
def send_message(parameters: dict, response: str | None = None, player=None, session_memory=None) -> bool:
|
||||
"""
|
||||
Windows uygulaması aracılığıyla mesaj gönderme sistemi (WhatsApp, Telegram, vb.)
|
||||
|
||||
Çoklu adım desteği:
|
||||
- Eksik bir parametre varsa sorarak devam eder.
|
||||
|
||||
Beklenen parametreler:
|
||||
- receiver (str)
|
||||
- message_text (str)
|
||||
- platform (str, Varsayılan: "WhatsApp")
|
||||
"""
|
||||
|
||||
if session_memory is None:
|
||||
msg = "Efendim, geçici hafıza hatası oluştu. İşlem yapılamıyor."
|
||||
if player:
|
||||
player.write_log(msg)
|
||||
edge_speak(msg, player)
|
||||
return False
|
||||
|
||||
|
||||
if parameters:
|
||||
session_memory.update_parameters(parameters)
|
||||
|
||||
|
||||
for param in REQUIRED_PARAMS:
|
||||
value = session_memory.get_parameter(param)
|
||||
if not value:
|
||||
|
||||
session_memory.set_current_question(param)
|
||||
question_text = ""
|
||||
|
||||
if param == "receiver":
|
||||
question_text = "Efendim, mesajı kime göndereyim?"
|
||||
elif param == "message_text":
|
||||
question_text = "Efendim, ne mesaj göndereyim?"
|
||||
elif param == "platform":
|
||||
question_text = "Efendim, hangi platformu kullanayım? (WhatsApp, Telegram, vb.)"
|
||||
else:
|
||||
question_text = f"Efendim, lütfen {param} bilgisini belirtin."
|
||||
|
||||
if player:
|
||||
player.write_log(f"AI: {question_text}")
|
||||
edge_speak(question_text, player)
|
||||
return False
|
||||
|
||||
|
||||
receiver = session_memory.get_parameter("receiver").strip()
|
||||
platform = session_memory.get_parameter("platform").strip() or "WhatsApp"
|
||||
message_text = session_memory.get_parameter("message_text").strip()
|
||||
|
||||
if response:
|
||||
if player:
|
||||
player.write_log(response)
|
||||
edge_speak(response, player)
|
||||
|
||||
try:
|
||||
pyautogui.PAUSE = 0.1
|
||||
|
||||
pyautogui.press("win")
|
||||
time.sleep(0.3)
|
||||
pyautogui.write(platform, interval=0.03)
|
||||
pyautogui.press("enter")
|
||||
time.sleep(0.6)
|
||||
|
||||
pyautogui.hotkey("ctrl", "f")
|
||||
time.sleep(0.2)
|
||||
pyautogui.write(receiver, interval=0.03)
|
||||
time.sleep(0.2)
|
||||
pyautogui.press("enter")
|
||||
time.sleep(0.2)
|
||||
|
||||
pyperclip.copy(message_text)
|
||||
time.sleep(0.1)
|
||||
pyautogui.hotkey("ctrl", "v")
|
||||
pyautogui.press("enter")
|
||||
|
||||
session_memory.clear_current_question()
|
||||
session_memory.clear_pending_intent()
|
||||
session_memory.update_parameters({})
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
msg = f"Efendim, mesajı gönderirken bir sorun oluştu. Hata: ({e})"
|
||||
if player:
|
||||
player.write_log(msg)
|
||||
edge_speak(msg, player)
|
||||
return False
|
||||
@@ -0,0 +1,67 @@
|
||||
import webbrowser
|
||||
from urllib.parse import quote_plus
|
||||
from tts import edge_speak
|
||||
|
||||
|
||||
def weather_action(
|
||||
parameters: dict,
|
||||
player=None,
|
||||
session_memory=None
|
||||
):
|
||||
"""
|
||||
Hava durumu eylemi.
|
||||
Google hava durumu aramasını açar ve kısa bir sesli onay verir.
|
||||
"""
|
||||
|
||||
city = parameters.get("city")
|
||||
time = parameters.get("time")
|
||||
if not city or not isinstance(city, str):
|
||||
msg = "Efendim, hava durumu raporu için şehir bilgisi eksik."
|
||||
_speak_and_log(msg, player)
|
||||
return msg
|
||||
|
||||
city = city.strip()
|
||||
|
||||
if not time or not isinstance(time, str):
|
||||
time = "bugün"
|
||||
else:
|
||||
time = time.strip()
|
||||
|
||||
search_query = f"{city} hava durumu {time}"
|
||||
encoded_query = quote_plus(search_query)
|
||||
url = f"https://www.google.com/search?q={encoded_query}"
|
||||
|
||||
try:
|
||||
webbrowser.open(url)
|
||||
except Exception:
|
||||
msg = "Efendim, hava durumu için tarayıcıyı açamadım."
|
||||
_speak_and_log(msg, player)
|
||||
return msg
|
||||
|
||||
msg = f"{city} için {time} hava durumunu gösteriyorum, efendim."
|
||||
_speak_and_log(msg, player)
|
||||
|
||||
if session_memory:
|
||||
try:
|
||||
session_memory.set_last_search(
|
||||
query=search_query,
|
||||
response=msg
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return msg
|
||||
|
||||
|
||||
def _speak_and_log(message: str, player=None):
|
||||
"""Yardımcı: güvenli şekilde loglama + TTS"""
|
||||
if player:
|
||||
try:
|
||||
player.write_log(f"JARVIS: {message}")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
edge_speak(message)
|
||||
except Exception:
|
||||
pass
|
||||
@@ -0,0 +1,164 @@
|
||||
import re
|
||||
from serpapi import GoogleSearch
|
||||
from tts import edge_speak
|
||||
|
||||
|
||||
MAX_SNIPPETS = 3
|
||||
MIN_SENTENCE_LENGTH = 30
|
||||
|
||||
|
||||
def clean(text: str) -> str:
|
||||
"""Metni temizle: fazla boşlukları, ..., parantezleri kaldır."""
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
text = re.sub(r"\s+", " ", text)
|
||||
text = re.sub(r"\.\.\.+", ".", text)
|
||||
text = re.sub(r"\[.*?\]", "", text)
|
||||
text = re.sub(r"\(.*?\)", "", text)
|
||||
return text.strip()
|
||||
|
||||
|
||||
def split_sentences(text: str):
|
||||
"""Metni cümlelere böl ve çok kısa ya da eksik olanları alma."""
|
||||
sentences = re.split(r"(?<=[.!?])\s+", text)
|
||||
|
||||
valid_sentences = []
|
||||
buffer = ""
|
||||
|
||||
for s in sentences:
|
||||
s = clean(s)
|
||||
if not s:
|
||||
continue
|
||||
|
||||
last_word = s.split()[-1].lower()
|
||||
if last_word in [
|
||||
"-nun", "içinde", "da", "üstünde", "tarafından",
|
||||
"sonra", "önce", "ile", "için", "-a"
|
||||
]:
|
||||
buffer += s + " "
|
||||
continue
|
||||
|
||||
if buffer:
|
||||
s = buffer + s
|
||||
buffer = ""
|
||||
|
||||
if len(s) >= MIN_SENTENCE_LENGTH:
|
||||
valid_sentences.append(s)
|
||||
|
||||
return valid_sentences
|
||||
|
||||
|
||||
def is_noise(text: str) -> bool:
|
||||
"""Bir cümlenin gürültü / alakasız olup olmadığını kontrol et."""
|
||||
t = text.lower()
|
||||
|
||||
noise_keywords = [
|
||||
"devamını oku",
|
||||
"daha fazla öğren",
|
||||
"buraya tıkla",
|
||||
"infografik",
|
||||
"google trendleri",
|
||||
"yılın aramaları",
|
||||
"en çok aranan",
|
||||
"ilk 100",
|
||||
"abone ol",
|
||||
"bunu paylaş",
|
||||
"reklam",
|
||||
"reklam:"
|
||||
]
|
||||
|
||||
return any(word in t for word in noise_keywords)
|
||||
|
||||
|
||||
def select_best_sentence(snippets):
|
||||
"""En fazla MAX_SNIPPETS birleştir ve tek, tutarlı bir cümle döndür."""
|
||||
final_text = ""
|
||||
count = 0
|
||||
|
||||
for snippet in snippets:
|
||||
if not snippet or is_noise(snippet):
|
||||
continue
|
||||
|
||||
sentences = split_sentences(snippet)
|
||||
|
||||
for s in sentences:
|
||||
if is_noise(s):
|
||||
continue
|
||||
|
||||
final_text = f"{final_text} {s}".strip()
|
||||
count += 1
|
||||
|
||||
if count >= MAX_SNIPPETS:
|
||||
return final_text
|
||||
|
||||
return final_text if final_text else None
|
||||
|
||||
|
||||
def serpapi_answer(query: str, api_key: str) -> str:
|
||||
"""Arama yap ve tek, tutarlı bir cümle döndür."""
|
||||
params = {
|
||||
"q": query,
|
||||
"engine": "google",
|
||||
"hl": "tr",
|
||||
"gl": "tr",
|
||||
"num": 3,
|
||||
"api_key": api_key
|
||||
}
|
||||
|
||||
try:
|
||||
data = GoogleSearch(params).get_dict()
|
||||
except Exception:
|
||||
return "Efendim, web araması başarısız oldu."
|
||||
|
||||
organic = data.get("organic_results", [])
|
||||
if not organic:
|
||||
return "Efendim, alakalı bir bilgi bulamadım."
|
||||
|
||||
snippets = [
|
||||
r.get("snippet", "")
|
||||
for r in organic
|
||||
if r.get("snippet")
|
||||
]
|
||||
|
||||
answer = select_best_sentence(snippets)
|
||||
|
||||
if not answer:
|
||||
return "Efendim, internette bilgi buldum ancak net bir şekilde özetleyemedim."
|
||||
|
||||
return answer
|
||||
|
||||
|
||||
def web_search(
|
||||
parameters: dict,
|
||||
player=None,
|
||||
session_memory=None,
|
||||
api_key: str = ""
|
||||
):
|
||||
"""
|
||||
Ana web araması:
|
||||
- 1 adet tutarlı cümle döndürür
|
||||
- Önceki cevapları eklemez
|
||||
- Yarım kalan cümleleri önlemek için birden fazla snippet birleştirir
|
||||
"""
|
||||
|
||||
query = (parameters or {}).get("query", "").strip()
|
||||
|
||||
if not query:
|
||||
msg = "Efendim, arama isteğini anlayamadım."
|
||||
if player:
|
||||
player.write_log(msg)
|
||||
edge_speak(msg)
|
||||
return msg
|
||||
|
||||
answer = serpapi_answer(query, api_key)
|
||||
|
||||
if player:
|
||||
player.write_log(f"Yapay Zeka: {answer}")
|
||||
|
||||
edge_speak(answer)
|
||||
|
||||
if session_memory:
|
||||
session_memory.set_last_search(query, answer)
|
||||
|
||||
return answer
|
||||
@@ -0,0 +1,64 @@
|
||||
Sen Jarvis’sin, tamamen optimize edilmiş, hızlı tepki veren bir Yapay Zekâ Bilgisayar Asistanısın.
|
||||
|
||||
Intents (Niyetler):
|
||||
- send_message
|
||||
- open_app
|
||||
- search
|
||||
- weather_report
|
||||
- chat
|
||||
|
||||
Parameter Rules (Parametre Kuralları):
|
||||
- search → parameters.query (temizlenmiş ana soru).
|
||||
- weather_report → city, time (time = gün).
|
||||
- send_message → receiver, message_text, platform (varsayılan: WhatsApp).
|
||||
Örnek:
|
||||
User: "Yusuf'a mesaj atabilir misin bu akşamki halısaha maçına geliyor muymuş diye?"
|
||||
DOĞRU message_text:
|
||||
"Bu akşamki halısaha maçına geliyor musun?"
|
||||
- Preserve all Turkish characters exactly as spoken (ç, ş, ı, ü, ö, ğ).
|
||||
- when intent is send_message, return text null, message text is enough.
|
||||
|
||||
- open_app → app_name
|
||||
(Eğer kullanıcı “açar mısın” gibi bir ifade kullanırsa,
|
||||
kısa ve hoş bir metin oluştur ve istenen uygulamayı aç.)
|
||||
- Eğer kullanıcı uygulama adını yanlış söylerse,
|
||||
doğru uygulamayı bularak aç
|
||||
(örn: “Hey Jarvis open what’s up” → WhatsApp).
|
||||
- Zorunlu bir alan eksikse →
|
||||
tek kısa netleştirme sorusu sor,
|
||||
needs_clarification:true ayarla ve dur.
|
||||
|
||||
Chat Mode (Sohbet Modu):
|
||||
- 1–2 kısa cümle, ekstra analiz yok.
|
||||
- Sadece şunları tut:
|
||||
- kimlik
|
||||
- tercihler
|
||||
- ilişkiler
|
||||
- duygusal_durum
|
||||
- Geçici hafıza (temporary memory).
|
||||
|
||||
Output format (Çıktı formatı):
|
||||
|
||||
json
|
||||
{
|
||||
"intent": null,
|
||||
"parameters": {},
|
||||
"needs_clarification": false,
|
||||
"text": null,
|
||||
"memory_update": {
|
||||
"identity": {},
|
||||
"preferences": {},
|
||||
"relationships": {},
|
||||
"emotional_state": {}
|
||||
}
|
||||
}
|
||||
|
||||
Hafızaya sadece gerçekten önemli bilgileri ekle:
|
||||
- Kullanıcının adı, yaşı, şehri falan
|
||||
- Kullanıcının sorduğu soruların çoğu hafızaya dahil edilmez.
|
||||
|
||||
Her zaman JSON formatında döndür.
|
||||
Text alanı senin mesajındır.
|
||||
Yanıtları çok hızlı üret.
|
||||
Kısa ve net ol.
|
||||
"Efendim" eklemeyi unutma Jarvis gibi
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 383 KiB |
@@ -0,0 +1,170 @@
|
||||
# llm.py
|
||||
|
||||
import os
|
||||
import json
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
|
||||
OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"
|
||||
|
||||
MODEL = "arcee-ai/trinity-large-preview:free"
|
||||
|
||||
PROMPT_PATH = "core/prompt.txt"
|
||||
|
||||
|
||||
def load_system_prompt() -> str:
|
||||
try:
|
||||
with open(PROMPT_PATH, "r", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
except Exception as e:
|
||||
print(f"⚠️ prompt.txt cannot installed: {e}")
|
||||
return "Senin ismin Jarvis, yardımcı olan bir yapay zeka asistanı"
|
||||
|
||||
|
||||
SYSTEM_PROMPT = load_system_prompt()
|
||||
|
||||
|
||||
def safe_json_parse(text: str) -> dict | None:
|
||||
if not text:
|
||||
return None
|
||||
|
||||
text = text.strip()
|
||||
|
||||
if "```json" in text:
|
||||
try:
|
||||
start = text.index("```json") + 7
|
||||
end = text.index("```", start)
|
||||
text = text[start:end].strip()
|
||||
except:
|
||||
pass
|
||||
elif "```" in text:
|
||||
try:
|
||||
start = text.index("```") + 3
|
||||
end = text.index("```", start)
|
||||
text = text[start:end].strip()
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
start = text.index("{")
|
||||
end = text.rindex("}") + 1
|
||||
json_str = text[start:end]
|
||||
return json.loads(json_str)
|
||||
except Exception as e:
|
||||
print(f"⚠️ JSON parse error: {e}")
|
||||
print(f"The error text: {text[:200]}")
|
||||
return None
|
||||
|
||||
|
||||
def get_llm_output(user_text: str, memory_block: dict = None) -> dict:
|
||||
|
||||
if not user_text or not user_text.strip():
|
||||
return {
|
||||
"intent": "chat",
|
||||
"parameters": {},
|
||||
"needs_clarification": False,
|
||||
"text": "Efendim, bunu anlayamadım",
|
||||
"memory_update": None
|
||||
}
|
||||
|
||||
if not OPENROUTER_API_KEY:
|
||||
print("❌ OPENROUTER_API_KEY bulunamadı!")
|
||||
return {
|
||||
"intent": "chat",
|
||||
"parameters": {},
|
||||
"text": "API key eklemeyi unuttunuz, efendim.",
|
||||
"needs_clarification": False,
|
||||
"memory_update": None
|
||||
}
|
||||
|
||||
# Memory'yi string'e çevir
|
||||
memory_str = ""
|
||||
if memory_block:
|
||||
memory_str = "\n".join(f"{k}: {v}" for k, v in memory_block.items())
|
||||
|
||||
user_prompt = f"""User message: "{user_text}"
|
||||
|
||||
Known user memory:
|
||||
{memory_str if memory_str else "No memory available"}"""
|
||||
|
||||
payload = {
|
||||
"model": MODEL,
|
||||
"messages": [
|
||||
{"role": "system", "content": SYSTEM_PROMPT},
|
||||
{"role": "user", "content": user_prompt}
|
||||
],
|
||||
"temperature": 0.2,
|
||||
"max_tokens": 500
|
||||
}
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
||||
"Content-Type": "application/json",
|
||||
"HTTP-Referer": "http://localhost",
|
||||
"X-Title": "Jarvis-Assistant"
|
||||
}
|
||||
|
||||
try:
|
||||
|
||||
response = requests.post(
|
||||
OPENROUTER_URL,
|
||||
headers=headers,
|
||||
json=payload,
|
||||
timeout=30
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
print(f"❌ API Hatası: {response.text}")
|
||||
return {
|
||||
"intent": "chat",
|
||||
"parameters": {},
|
||||
"text": f"Sir, API error: {response.status_code}",
|
||||
"needs_clarification": False,
|
||||
"memory_update": None
|
||||
}
|
||||
|
||||
data = response.json()
|
||||
content = data["choices"][0]["message"]["content"]
|
||||
|
||||
# JSON parse et
|
||||
parsed = safe_json_parse(content)
|
||||
|
||||
if parsed:
|
||||
return {
|
||||
"intent": parsed.get("intent", "chat"),
|
||||
"parameters": parsed.get("parameters", {}),
|
||||
"needs_clarification": parsed.get("needs_clarification", False),
|
||||
"text": parsed.get("text"),
|
||||
"memory_update": parsed.get("memory_update")
|
||||
}
|
||||
|
||||
return {
|
||||
"intent": "chat",
|
||||
"parameters": {},
|
||||
"needs_clarification": False,
|
||||
"text": content,
|
||||
"memory_update": None
|
||||
}
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
print("❌ API timeout!")
|
||||
return {
|
||||
"intent": "chat",
|
||||
"text": "Efendim, bağlantı süresi aşıma uğradı.",
|
||||
"parameters": {},
|
||||
"needs_clarification": False,
|
||||
"memory_update": None
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ LLM ERROR: {e}")
|
||||
return {
|
||||
"intent": "chat",
|
||||
"text": "Efendim, bir sistem hatasıyla karşılaştım.",
|
||||
"parameters": {},
|
||||
"needs_clarification": False,
|
||||
"memory_update": None
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
import asyncio
|
||||
import threading
|
||||
|
||||
from speech_to_text import record_voice, stop_listening_flag
|
||||
from llm import get_llm_output
|
||||
from tts import edge_speak, stop_speaking
|
||||
from ui import JarvisUI
|
||||
|
||||
from actions.open_app import open_app
|
||||
from actions.web_search import web_search
|
||||
from actions.weather_report import weather_action
|
||||
from actions.send_message import send_message
|
||||
|
||||
from memory.memory_manager import load_memory, update_memory
|
||||
from memory.temporary_memory import TemporaryMemory
|
||||
|
||||
interrupt_commands = ["sessiz", "çık", "bitir", "dur"]
|
||||
|
||||
|
||||
temp_memory = TemporaryMemory()
|
||||
|
||||
|
||||
async def get_voice_input():
|
||||
return await asyncio.to_thread(record_voice)
|
||||
|
||||
|
||||
async def ai_loop(ui: JarvisUI):
|
||||
while True:
|
||||
stop_listening_flag.clear()
|
||||
user_text = await get_voice_input()
|
||||
|
||||
if not user_text:
|
||||
continue
|
||||
|
||||
|
||||
if any(cmd in user_text.lower() for cmd in interrupt_commands):
|
||||
stop_speaking()
|
||||
temp_memory.reset()
|
||||
continue
|
||||
|
||||
ui.write_log(f"You: {user_text}")
|
||||
|
||||
|
||||
if temp_memory.get_current_question():
|
||||
param = temp_memory.get_current_question()
|
||||
temp_memory.update_parameters({param: user_text})
|
||||
temp_memory.clear_current_question()
|
||||
user_text = temp_memory.get_last_user_text()
|
||||
|
||||
temp_memory.set_last_user_text(user_text)
|
||||
|
||||
long_term_memory = load_memory()
|
||||
|
||||
def minimal_memory_for_prompt(memory: dict) -> dict:
|
||||
result = {}
|
||||
identity = memory.get("identity", {})
|
||||
preferences = memory.get("preferences", {})
|
||||
relationships = memory.get("relationships", {})
|
||||
emotional_state = memory.get("emotional_state", {})
|
||||
|
||||
if "name" in identity:
|
||||
result["user_name"] = identity["name"].get("value")
|
||||
|
||||
for k in ["favorite_color", "favorite_food", "favorite_music"]:
|
||||
if k in preferences:
|
||||
val = preferences[k].get("value")
|
||||
if isinstance(val, dict) and "value" in val:
|
||||
val = val["value"]
|
||||
result[k] = val
|
||||
|
||||
for rel, info in relationships.items():
|
||||
if isinstance(info, dict) and "name" in info and "value" in info["name"]:
|
||||
result[f"{rel}_name"] = info["name"]["value"]
|
||||
|
||||
for event, info in emotional_state.items():
|
||||
if "value" in info:
|
||||
result[f"emotion_{event}"] = info["value"]
|
||||
|
||||
return {k: v for k, v in result.items() if v}
|
||||
|
||||
memory_for_prompt = minimal_memory_for_prompt(long_term_memory)
|
||||
|
||||
history_lines = temp_memory.get_history_for_prompt()
|
||||
recent_history = "\n".join(history_lines.split("\n")[-5:])
|
||||
if recent_history:
|
||||
memory_for_prompt["recent_conversation"] = recent_history
|
||||
|
||||
if temp_memory.has_pending_intent():
|
||||
memory_for_prompt["_pending_intent"] = temp_memory.pending_intent
|
||||
memory_for_prompt["_collected_params"] = str(temp_memory.get_parameters())
|
||||
|
||||
try:
|
||||
llm_output = get_llm_output(
|
||||
user_text=user_text,
|
||||
memory_block=memory_for_prompt
|
||||
)
|
||||
except Exception as e:
|
||||
ui.write_log(f"AI ERROR: {e}")
|
||||
continue
|
||||
|
||||
intent = llm_output.get("intent", "chat")
|
||||
parameters = llm_output.get("parameters", {})
|
||||
response = llm_output.get("text")
|
||||
memory_update = llm_output.get("memory_update")
|
||||
|
||||
if memory_update and isinstance(memory_update, dict):
|
||||
update_memory(memory_update)
|
||||
|
||||
temp_memory.set_last_ai_response(response)
|
||||
|
||||
if intent == "send_message":
|
||||
temp_memory.set_pending_intent("send_message")
|
||||
temp_memory.update_parameters(parameters)
|
||||
|
||||
all_params_ready = all(temp_memory.get_parameter(p) for p in ["receiver", "message_text", "platform"])
|
||||
if all_params_ready:
|
||||
threading.Thread(
|
||||
target=send_message,
|
||||
kwargs={
|
||||
"parameters": temp_memory.get_parameters(),
|
||||
"response": response,
|
||||
"player": ui,
|
||||
"session_memory": temp_memory
|
||||
},
|
||||
daemon=True
|
||||
).start()
|
||||
|
||||
elif intent == "open_app":
|
||||
app_name = parameters.get("app_name")
|
||||
if app_name:
|
||||
threading.Thread(
|
||||
target=open_app,
|
||||
kwargs={
|
||||
"parameters": parameters,
|
||||
"response": response,
|
||||
"player": ui,
|
||||
"session_memory": temp_memory
|
||||
},
|
||||
daemon=True
|
||||
).start()
|
||||
|
||||
elif intent == "weather_report":
|
||||
city = parameters.get("city")
|
||||
time = parameters.get("time")
|
||||
if city:
|
||||
threading.Thread(
|
||||
target=weather_action,
|
||||
kwargs={
|
||||
"parameters": parameters,
|
||||
"player": ui,
|
||||
"session_memory": temp_memory
|
||||
},
|
||||
daemon=True
|
||||
).start()
|
||||
|
||||
elif intent == "search":
|
||||
query = parameters.get("query")
|
||||
if query:
|
||||
threading.Thread(
|
||||
target=web_search,
|
||||
kwargs={
|
||||
"parameters": parameters,
|
||||
"player": ui,
|
||||
"session_memory": temp_memory,
|
||||
"api_key": "SERP API ANAHTARINIZI ALIN"
|
||||
},
|
||||
daemon=True
|
||||
).start()
|
||||
|
||||
else:
|
||||
if response:
|
||||
ui.write_log(f"AI: {response}")
|
||||
edge_speak(response, ui)
|
||||
|
||||
await asyncio.sleep(0.01)
|
||||
|
||||
|
||||
def main():
|
||||
ui = JarvisUI("face.png", size=(900, 900))
|
||||
|
||||
def runner():
|
||||
asyncio.run(ai_loop(ui))
|
||||
|
||||
threading.Thread(target=runner, daemon=True).start()
|
||||
ui.root.mainloop()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1 @@
|
||||
#memory package
|
||||
@@ -0,0 +1,83 @@
|
||||
# memory/memory_manager.py
|
||||
import json
|
||||
import os
|
||||
from threading import Lock
|
||||
from datetime import datetime
|
||||
|
||||
MEMORY_PATH = "memory/memory.json"
|
||||
_lock = Lock()
|
||||
|
||||
|
||||
def _empty_memory() -> dict:
|
||||
"""Return an empty memory structure."""
|
||||
return {
|
||||
"identity": {},
|
||||
"preferences": {},
|
||||
"relationships": {},
|
||||
"emotional_state": {}
|
||||
}
|
||||
|
||||
|
||||
def load_memory() -> dict:
|
||||
"""Load memory from disk, return empty if not exists or invalid."""
|
||||
if not os.path.exists(MEMORY_PATH):
|
||||
return _empty_memory()
|
||||
|
||||
with _lock:
|
||||
try:
|
||||
with open(MEMORY_PATH, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
if isinstance(data, dict):
|
||||
return data
|
||||
return _empty_memory()
|
||||
except Exception:
|
||||
return _empty_memory()
|
||||
|
||||
|
||||
def save_memory(memory: dict) -> None:
|
||||
"""Save memory to disk safely."""
|
||||
if not isinstance(memory, dict):
|
||||
return
|
||||
|
||||
os.makedirs(os.path.dirname(MEMORY_PATH), exist_ok=True)
|
||||
|
||||
with _lock:
|
||||
with open(MEMORY_PATH, "w", encoding="utf-8") as f:
|
||||
json.dump(memory, f, indent=2, ensure_ascii=False)
|
||||
|
||||
|
||||
def _recursive_update(target: dict, updates: dict) -> bool:
|
||||
"""Recursively merge updates into target memory. Returns True if changed."""
|
||||
changed = False
|
||||
now = datetime.utcnow().isoformat() + "Z"
|
||||
|
||||
for key, value in updates.items():
|
||||
if value is None or (isinstance(value, str) and not value.strip()):
|
||||
continue
|
||||
|
||||
if isinstance(value, dict) and "value" not in value:
|
||||
if key not in target or not isinstance(target[key], dict):
|
||||
target[key] = {}
|
||||
changed = True
|
||||
if _recursive_update(target[key], value):
|
||||
changed = True
|
||||
else:
|
||||
|
||||
entry = value if isinstance(value, dict) and "value" in value else {"value": value}
|
||||
if key not in target or target[key] != entry:
|
||||
target[key] = entry
|
||||
changed = True
|
||||
|
||||
return changed
|
||||
|
||||
|
||||
def update_memory(memory_update: dict) -> dict:
|
||||
"""Merge LLM memory update into global memory and save."""
|
||||
if not isinstance(memory_update, dict):
|
||||
return load_memory()
|
||||
|
||||
memory = load_memory()
|
||||
if _recursive_update(memory, memory_update):
|
||||
save_memory(memory)
|
||||
|
||||
return memory
|
||||
@@ -0,0 +1,136 @@
|
||||
# memory/temporary_memory.py
|
||||
from typing import Any
|
||||
|
||||
|
||||
class TemporaryMemory:
|
||||
"""
|
||||
Temporary runtime memory (session-only).
|
||||
Resets when program restarts.
|
||||
|
||||
Purpose:
|
||||
- Track multi-step intents
|
||||
- Keep short conversation context
|
||||
- Store last actions (search / open_app)
|
||||
- Support follow-up questions like:
|
||||
"why did he executed?"
|
||||
"""
|
||||
|
||||
def __init__(self, max_history: int = 5):
|
||||
self.max_history = max_history
|
||||
self.reset()
|
||||
|
||||
|
||||
def reset(self):
|
||||
self.pending_intent: str | None = None
|
||||
self.parameters: dict[str, Any] = {}
|
||||
self.current_question: str | None = None
|
||||
|
||||
self.last_user_text: str | None = None
|
||||
self.last_ai_response: str | None = None
|
||||
|
||||
# --- Action memory ---
|
||||
self.last_search: dict | None = None
|
||||
self.last_opened_app: str | None = None
|
||||
|
||||
# --- Conversation ---
|
||||
self.conversation_history: list[dict[str, str]] = []
|
||||
|
||||
|
||||
def set_pending_intent(self, intent: str):
|
||||
self.pending_intent = intent
|
||||
|
||||
def clear_pending_intent(self):
|
||||
self.pending_intent = None
|
||||
self.parameters = {}
|
||||
self.current_question = None
|
||||
|
||||
def has_pending_intent(self) -> bool:
|
||||
return self.pending_intent is not None
|
||||
|
||||
|
||||
def update_parameters(self, new_params: dict):
|
||||
if not isinstance(new_params, dict):
|
||||
return
|
||||
for k, v in new_params.items():
|
||||
if v not in (None, ""):
|
||||
self.parameters[k] = v
|
||||
|
||||
def get_parameters(self) -> dict:
|
||||
return self.parameters.copy()
|
||||
|
||||
def get_parameter(self, key: str):
|
||||
return self.parameters.get(key)
|
||||
|
||||
def set_current_question(self, param_name: str):
|
||||
self.current_question = param_name
|
||||
|
||||
def get_current_question(self) -> str | None:
|
||||
return self.current_question
|
||||
|
||||
def clear_current_question(self):
|
||||
self.current_question = None
|
||||
|
||||
def set_last_user_text(self, text: str):
|
||||
self.last_user_text = text
|
||||
self._add_to_history("user", text)
|
||||
|
||||
def set_last_ai_response(self, text: str):
|
||||
self.last_ai_response = text
|
||||
self._add_to_history("ai", text)
|
||||
|
||||
def get_last_user_text(self):
|
||||
return self.last_user_text
|
||||
|
||||
def get_last_ai_response(self):
|
||||
return self.last_ai_response
|
||||
|
||||
|
||||
def set_last_search(self, query: str, answer: str):
|
||||
self.last_search = {
|
||||
"query": query,
|
||||
"answer": answer
|
||||
}
|
||||
|
||||
def get_last_search(self):
|
||||
return self.last_search
|
||||
|
||||
def set_open_app(self, app_name: str):
|
||||
self.last_opened_app = app_name
|
||||
|
||||
def get_last_opened_app(self):
|
||||
return self.last_opened_app
|
||||
|
||||
def _add_to_history(self, role: str, text: str):
|
||||
if role not in ("user", "ai"):
|
||||
return
|
||||
|
||||
self.conversation_history.append({
|
||||
"role": role,
|
||||
"text": text
|
||||
})
|
||||
|
||||
if len(self.conversation_history) > self.max_history:
|
||||
self.conversation_history.pop(0)
|
||||
|
||||
def get_history_for_prompt(self) -> str:
|
||||
"""
|
||||
Returns compact history for LLM prompt.
|
||||
"""
|
||||
return "\n".join(
|
||||
f"{m['role'].capitalize()}: {m['text']}"
|
||||
for m in self.conversation_history
|
||||
)
|
||||
|
||||
|
||||
def get_context_summary(self) -> dict:
|
||||
"""
|
||||
Useful for debugging or smarter LLM prompts.
|
||||
"""
|
||||
return {
|
||||
"pending_intent": self.pending_intent,
|
||||
"parameters": self.parameters,
|
||||
"last_search": self.last_search,
|
||||
"last_opened_app": self.last_opened_app,
|
||||
"last_user_text": self.last_user_text,
|
||||
"last_ai_response": self.last_ai_response,
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import sounddevice as sd
|
||||
import vosk
|
||||
import queue
|
||||
import sys
|
||||
import json
|
||||
import threading
|
||||
|
||||
MODEL_PATH = "C:/Users/90553/Downloads/vosktr/vosk-model-small-tr-0.3" # put your path
|
||||
model = vosk.Model(MODEL_PATH)
|
||||
|
||||
q = queue.Queue()
|
||||
stop_listening_flag = threading.Event()
|
||||
|
||||
def callback(indata, frames, time, status):
|
||||
if status:
|
||||
print(status, file=sys.stderr)
|
||||
q.put(bytes(indata))
|
||||
|
||||
def record_voice(prompt="🎙 Sizi dinliyorum efendim."):
|
||||
"""
|
||||
Blocking call, returns the first recognized sentence.
|
||||
"""
|
||||
print(prompt)
|
||||
rec = vosk.KaldiRecognizer(model, 16000)
|
||||
with sd.RawInputStream(samplerate=16000, blocksize=8000, dtype='int16',
|
||||
channels=1, callback=callback):
|
||||
while not stop_listening_flag.is_set():
|
||||
try:
|
||||
data = q.get(timeout=0.1)
|
||||
except queue.Empty:
|
||||
continue
|
||||
if rec.AcceptWaveform(data):
|
||||
result = json.loads(rec.Result())
|
||||
text = result.get("text", "")
|
||||
if text.strip():
|
||||
print("👤 You:", text)
|
||||
return text
|
||||
return ""
|
||||
@@ -0,0 +1,71 @@
|
||||
import io
|
||||
import threading
|
||||
import requests
|
||||
import sounddevice as sd
|
||||
import soundfile as sf
|
||||
|
||||
#Take the API key from ElevenLabs
|
||||
|
||||
ELEVEN_API_KEY = "ELEVELABS ANAHTARINIZI ALIN"
|
||||
VOICE_ID = "JBFqnCBsd6RMkjVDRZzb"
|
||||
|
||||
stop_speaking_flag = threading.Event()
|
||||
|
||||
def edge_speak(text: str, ui=None, blocking=False):
|
||||
if not text.strip():
|
||||
return
|
||||
|
||||
|
||||
finished_event = threading.Event()
|
||||
|
||||
def _thread():
|
||||
if ui:
|
||||
ui.start_speaking()
|
||||
stop_speaking_flag.clear()
|
||||
|
||||
try:
|
||||
url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"
|
||||
headers = {
|
||||
"xi-api-key": ELEVEN_API_KEY,
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
payload = {
|
||||
"text": text.strip(),
|
||||
"voice_settings": {
|
||||
"stability": 0.55,
|
||||
"similarity_boost": 0.85
|
||||
}
|
||||
}
|
||||
|
||||
response = requests.post(url, json=payload, headers=headers)
|
||||
response.raise_for_status()
|
||||
|
||||
audio_data = io.BytesIO(response.content)
|
||||
data, samplerate = sf.read(audio_data, dtype="float32")
|
||||
|
||||
channels = data.shape[1] if len(data.shape) > 1 else 1
|
||||
with sd.OutputStream(
|
||||
samplerate=samplerate,
|
||||
channels=channels,
|
||||
dtype="float32"
|
||||
) as stream:
|
||||
block_size = 1024
|
||||
for start in range(0, len(data), block_size):
|
||||
if stop_speaking_flag.is_set():
|
||||
break
|
||||
stream.write(data[start:start + block_size])
|
||||
|
||||
except Exception as e:
|
||||
print("SES HATASI:", e)
|
||||
finally:
|
||||
if ui:
|
||||
ui.stop_speaking()
|
||||
finished_event.set()
|
||||
|
||||
threading.Thread(target=_thread, daemon=True).start()
|
||||
|
||||
if blocking:
|
||||
finished_event.wait()
|
||||
|
||||
def stop_speaking():
|
||||
stop_speaking_flag.set()
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
import os
|
||||
import time
|
||||
import random
|
||||
import tkinter as tk
|
||||
from collections import deque
|
||||
from PIL import Image, ImageTk, ImageDraw, ImageFilter
|
||||
from tkinter.scrolledtext import ScrolledText
|
||||
|
||||
|
||||
class JarvisUI:
|
||||
def __init__(self, face_path, size=(760, 760)):
|
||||
self.root = tk.Tk()
|
||||
self.root.title("J.A.R.V.I.S")
|
||||
self.root.resizable(False, False)
|
||||
self.root.geometry("760x900")
|
||||
self.root.configure(bg="#000000")
|
||||
|
||||
self.size = size
|
||||
self.center_y = 0.42
|
||||
|
||||
self.canvas = tk.Canvas(
|
||||
self.root,
|
||||
width=size[0],
|
||||
height=size[1],
|
||||
bg="#000000",
|
||||
highlightthickness=0
|
||||
)
|
||||
self.canvas.place(relx=0.5, rely=self.center_y, anchor="center")
|
||||
|
||||
self.face_base = (
|
||||
Image.open(face_path)
|
||||
.convert("RGBA")
|
||||
.resize(size, Image.LANCZOS)
|
||||
)
|
||||
|
||||
self.halo_base = self._create_halo(size, radius=220, y_offset=-50)
|
||||
|
||||
self.speaking = False
|
||||
self.scale = 1.0
|
||||
self.target_scale = 1.0
|
||||
self.halo_alpha = 70
|
||||
self.target_halo_alpha = 70
|
||||
self.last_target_time = time.time()
|
||||
|
||||
self.text_box = ScrolledText(
|
||||
self.root,
|
||||
fg="#8ffcff",
|
||||
bg="#000000",
|
||||
insertbackground="#8ffcff",
|
||||
height=12,
|
||||
borderwidth=0,
|
||||
wrap="word",
|
||||
font=("Consolas", 10),
|
||||
padx=12,
|
||||
pady=12
|
||||
)
|
||||
self.text_box.place(relx=0.5, rely=0.86, anchor="center")
|
||||
self.text_box.configure(state="disabled")
|
||||
|
||||
self.typing_queue = deque()
|
||||
self.is_typing = False
|
||||
|
||||
self._animate()
|
||||
self.root.protocol("WM_DELETE_WINDOW", lambda: os._exit(0))
|
||||
|
||||
def _create_halo(self, size, radius, y_offset):
|
||||
w, h = size
|
||||
img = Image.new("RGBA", size, (0, 0, 0, 0))
|
||||
draw = ImageDraw.Draw(img)
|
||||
|
||||
cx = w // 2
|
||||
cy = h // 2 + y_offset
|
||||
|
||||
for r in range(radius, 0, -12):
|
||||
alpha = int(70 * (1 - r / radius))
|
||||
draw.ellipse(
|
||||
(cx - r, cy - r, cx + r, cy + r),
|
||||
fill=(0, 180, 255, alpha)
|
||||
)
|
||||
|
||||
return img.filter(ImageFilter.GaussianBlur(30))
|
||||
|
||||
def write_log(self, text: str):
|
||||
self.typing_queue.append(text)
|
||||
if not self.is_typing:
|
||||
self._start_typing()
|
||||
|
||||
def _start_typing(self):
|
||||
if not self.typing_queue:
|
||||
self.is_typing = False
|
||||
return
|
||||
|
||||
self.is_typing = True
|
||||
text = self.typing_queue.popleft()
|
||||
|
||||
self.text_box.configure(state="normal")
|
||||
self._type_char(text, 0)
|
||||
|
||||
def _type_char(self, text, i):
|
||||
if i < len(text):
|
||||
self.text_box.insert(tk.END, text[i])
|
||||
self.text_box.see(tk.END)
|
||||
self.root.after(12, self._type_char, text, i + 1)
|
||||
else:
|
||||
self.text_box.insert(tk.END, "\n")
|
||||
self.text_box.configure(state="disabled")
|
||||
self.root.after(40, self._start_typing)
|
||||
|
||||
def start_speaking(self):
|
||||
self.speaking = True
|
||||
|
||||
def stop_speaking(self):
|
||||
self.speaking = False
|
||||
|
||||
def _animate(self):
|
||||
now = time.time()
|
||||
|
||||
if now - self.last_target_time > (0.25 if self.speaking else 0.7):
|
||||
if self.speaking:
|
||||
self.target_scale = random.uniform(1.02, 1.1)
|
||||
self.target_halo_alpha = random.randint(120, 150)
|
||||
else:
|
||||
self.target_scale = random.uniform(1.004, 1.012)
|
||||
self.target_halo_alpha = random.randint(60, 80)
|
||||
|
||||
self.last_target_time = now
|
||||
|
||||
scale_speed = 0.45 if self.speaking else 0.25
|
||||
halo_speed = 0.40 if self.speaking else 0.25
|
||||
|
||||
self.scale += (self.target_scale - self.scale) * scale_speed
|
||||
self.halo_alpha += (self.target_halo_alpha - self.halo_alpha) * halo_speed
|
||||
|
||||
frame = Image.new("RGBA", self.size, (0, 0, 0, 255))
|
||||
|
||||
halo = self.halo_base.copy()
|
||||
halo.putalpha(int(self.halo_alpha))
|
||||
frame.alpha_composite(halo)
|
||||
|
||||
w, h = self.size
|
||||
face = self.face_base.resize(
|
||||
(int(w * self.scale), int(h * self.scale)),
|
||||
Image.LANCZOS
|
||||
)
|
||||
|
||||
fx = (w - face.size[0]) // 2
|
||||
fy = (h - face.size[1]) // 2
|
||||
frame.alpha_composite(face, (fx, fy))
|
||||
|
||||
img = ImageTk.PhotoImage(frame)
|
||||
self.canvas.delete("all")
|
||||
self.canvas.create_image(w // 2, h // 2, image=img)
|
||||
self.canvas.image = img
|
||||
|
||||
self.root.after(16, self._animate)
|
||||
Reference in new issue
Block a user