-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLlamaPi_gemini.py
74 lines (64 loc) · 1.93 KB
/
LlamaPi_gemini.py
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
69
70
71
72
73
74
import os
from pprint import pprint
import re
import signal
import socket
import sys
import threading
import tkinter as tk
from tkinter import scrolledtext
import pyaudio
import wave
import atexit
from faster_whisper import WhisperModel
import logging
import numpy as np
import soundfile as sf
import io
import subprocess
import opencc
from openai import OpenAI
import time
from PIL import Image, ImageTk
import google.generativeai as genai
from gemini import GeminiWrapper
from LlamaPi import LlamaPiBase
logging.basicConfig(
format='%(asctime)s [%(levelname)s] %(filename)s:%(funcName)s: %(message)s',
level=logging.DEBUG,
handlers=[
logging.StreamHandler() # Output logs to stdout
]
)
class LlamaPiGemini(LlamaPiBase):
def __init__(self):
super().__init__()
self.bot = None
self.window_title = "LlamaPi Robot on Gemini"
# Overrides the `prepare_llm` method in base class.
def prepare_llm(self):
self.bot = GeminiWrapper(api_key=os.environ["GEMINI_APIKEY"])
self.bot.new_chat_session(system_inst=self.system_msg["content"])
# Overrides the `llm` method in base class.
def llm(self, request, warmup=False) -> str:
if len(request) < 2:
logging.info("request empty or too short")
return
if not self.bot:
logging.info("No LLM bot available")
return
resp = self.bot.chat(request)
cmd = None
if resp:
self.append_to_text_box("\nAssistant: ")
# self.append_to_text_box(resp)
cmd = resp.split("$")[-1].strip()
voice = resp.split("$")[:-1]
voice = ' '.join(voice)
self.append_to_text_box(voice)
logging.info(f"Command word: {cmd}")
# self.append_to_text_box(f"\nCommand: {cmd}\n")
self.speak_back(voice)
return cmd
if __name__ == "__main__":
LlamaPiGemini().start()