File size: 5,737 Bytes
e294914
cab263c
d022aab
7c29ee2
1581bbf
7378fc8
 
e294914
 
d5436e0
 
 
f8dfb0f
d63135e
e294914
7378fc8
e294914
 
 
7378fc8
 
e294914
 
 
d022aab
7378fc8
 
d022aab
7c29ee2
 
7378fc8
7c29ee2
 
7378fc8
 
 
 
 
 
 
 
 
 
 
 
91b59ba
7378fc8
 
 
 
 
 
 
 
 
91b59ba
7378fc8
 
 
91b59ba
7c29ee2
42b403c
e294914
 
 
7378fc8
e294914
 
7378fc8
 
 
e294914
7378fc8
 
 
e294914
7378fc8
 
 
e294914
 
0f596d3
e294914
0f596d3
7378fc8
 
0f596d3
 
e294914
 
4affef3
 
d834bcd
 
 
 
d5436e0
e294914
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83cf282
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# version - ArcticMonkeys:30.07.24

# python core libraries
import re
import psutil
import time
import random
# streamlit
import streamlit as st
# components from other authors
from streamlit_mic_recorder import mic_recorder
# core modules
from audio_processing.A2T import A2T
from audio_processing.T2A import T2A
from llm.utils.chat import Conversation
from vlm.vlm import VLM
# utils modules
from utils.keywords import keywords
from utils.prompt_toggle import select_prompt, load_prompts
from utils.documentation import create_hand_gesture_doc
from utils.image_caption import ImageCaption

prompts = load_prompts()
chat = Conversation()
t2a = T2A()
vlm = VLM()
ic = ImageCaption()

def remove_labels_with_regex(text: str):
    pattern = r'^(Human:|AI:|Chelsea:)\s*'
    cleaned_text = re.sub(pattern, '', text, flags=re.MULTILINE)
    return cleaned_text

def exctrator(sentence, phrase="show me your image"):
    extracted_text = sentence.split(phrase)[1].strip() if phrase in sentence else ""
    return extracted_text

def switching(text):
    command = re.search("show me your image", text.lower(), re.IGNORECASE)
    result = None

    if command:
        prompt = exctrator(text.lower())
        # Завантажуємо зображення
        uploaded_image = ic.load_image()

        if uploaded_image is not None:
            # Якщо зображення завантажено, виконуємо обробку
            result = ic.send2ai(model=vlm, prompt=prompt)
        else:
            # Якщо зображення ще не завантажене, показуємо попередження
            st.warning("No image uploaded yet. Please upload an image to continue.")
    else:
        prompt = select_prompt(input_text=text, prompts=prompts, keywords=keywords)
        result = chat.chatting(prompt=prompt if prompt is not None else text)

    print(f"Prompt:\n{prompt}")
    prompt = None
    return result


def main():
    try:
        mic = mic_recorder(start_prompt="Record", stop_prompt="Stop", just_once=True, use_container_width=True)
        if mic is not None:
            start_time = time.perf_counter()
            a2t = A2T(mic["bytes"])
            text = a2t.predict()
            print(f"Text from A2T:\n{text}")
            execution_time = time.perf_counter() - start_time
            print(f"App.py -> main() -> time of execution A2T -> {execution_time}s")

            output = switching(text)
            response = remove_labels_with_regex(text=output)
            start_time_t2a = time.perf_counter()
            t2a.autoplay(response)
            execution_time_t2a = time.perf_counter() - start_time_t2a
            print(f"App.py -> main() -> time of execution T2A -> {execution_time_t2a}s")
            print(ic.pil_image)

            if response:
                st.markdown(f"Your input: {text}")
                st.markdown(f"Chelsea response: {response}")
                
                if ic.pil_image is not None:
                    st.image(ic.pil_image, caption="Uploaded Image", use_column_width=True)
                response = None
                
    except Exception as e:
        print(f"An error occurred in main finction, reasone is: {e}")

if __name__ == "__main__":
    print(f"Total Memory: {psutil.virtual_memory().total / (1024**3):.2f} GB")
    print(f"Available Memory: {psutil.virtual_memory().available / (1024**3):.2f} GB")
    print(f"CPU Cores: {psutil.cpu_count()}")
    print(f"CPU Usage: {psutil.cpu_percent()}%")
    main()
    footer="""
        <style>
            /* Common styles for the footer */
            .footer {
                position: fixed;
                left: 0;
                bottom: 0;
                width: 100%;
                height: 60px; /* Set a fixed height for consistency */
                font-size: 14px; /* Adjust font size for readability */
                text-align: center;
                padding: 15px 0; /* Reduced padding */
                transition: color 0.3s, background-color 0.3s;
            }
        
            .footer p {
                margin: 0; /* Remove default margins */
                font-size: 18px; /* Adjust font size as needed */
            }
        
            a:link, a:visited {
                text-decoration: dotted;
                color: inherit; /* Use current text color */
            }
        
            a:hover, a:active {
                background: linear-gradient(to right, #ffe44d, #ffdd1a, #ffd700, #ffd900);
                -webkit-text-fill-color: transparent;
                -webkit-background-clip: text;
            }
        
            .footer a:hover {
                color: #ff4500; /* Different hover color */
            }
        
            /* Light mode styles */
            @media (prefers-color-scheme: light) { 
                a:link, a:visited {
                    color: #0056b3; /* Blue color for links */
                }
        
                .footer a:hover {
                    color: #ff4500; /* Hover color for light mode */
                }
            }
        
            /* Dark mode styles */
            @media (prefers-color-scheme: dark) {        
                a:link, a:visited {
                    color: #ffd700; /* Gold color for links in dark mode */
                }
        
                .footer a:hover {
                    color: #ffa500; /* Hover color for dark mode */
                }
            }
        </style>
        
        <div class="footer">
            <p>Please support the project on <a href="https://buymeacoffee.com/cineai" target="_blank">Buy Me a Coffee</a></p>
        </div>
        """
    # st.markdown(footer,unsafe_allow_html=True)