Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@
|
|
3 |
# python core libraries
|
4 |
import re
|
5 |
import psutil
|
|
|
|
|
6 |
# streamlit
|
7 |
import streamlit as st
|
8 |
# components from other authors
|
@@ -11,66 +13,81 @@ from streamlit_mic_recorder import mic_recorder
|
|
11 |
from audio_processing.A2T import A2T
|
12 |
from audio_processing.T2A import T2A
|
13 |
from llm.utils.chat import Conversation
|
|
|
14 |
# utils modules
|
15 |
from utils.keywords import keywords
|
16 |
from utils.prompt_toggle import select_prompt, load_prompts
|
17 |
-
from utils.documentation import
|
18 |
-
|
19 |
-
# TODO:
|
20 |
-
# * Зробити в utils можливість для використання різних промптів -> Done
|
21 |
-
# * Додати як робив на HF хто на фото -> agent
|
22 |
-
# * Додати можливіть малюнками вирішувати мат проблеми -> agent
|
23 |
-
# * Додати моливість створювати/редагувати докменти(pdf, docx) -> agent
|
24 |
|
25 |
prompts = load_prompts()
|
26 |
-
doc = Documentation()
|
27 |
chat = Conversation()
|
28 |
t2a = T2A()
|
|
|
|
|
29 |
|
30 |
def remove_labels_with_regex(text: str):
|
31 |
-
removed_dub = remove_duplicates(text)
|
32 |
pattern = r'^(Human:|AI:|Chelsea:)\s*'
|
33 |
-
cleaned_text = re.sub(pattern, '',
|
34 |
return cleaned_text
|
35 |
|
36 |
-
def
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
result.
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
|
|
|
|
47 |
|
48 |
|
49 |
def main():
|
50 |
try:
|
51 |
mic = mic_recorder(start_prompt="Record", stop_prompt="Stop", just_once=True, use_container_width=True)
|
52 |
if mic is not None:
|
|
|
53 |
a2t = A2T(mic["bytes"])
|
54 |
text = a2t.predict()
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
response = remove_labels_with_regex(text=output_from_chat)
|
60 |
t2a.autoplay(response)
|
|
|
|
|
|
|
61 |
|
62 |
if response:
|
63 |
st.markdown(f"Your input: {text}")
|
64 |
st.markdown(f"Chelsea response: {response}")
|
65 |
|
66 |
-
|
|
|
67 |
response = None
|
68 |
-
|
69 |
-
print(f"Prompt: {prompt}, response: {response}")
|
70 |
|
71 |
except Exception as e:
|
72 |
print(f"An error occurred in main finction, reasone is: {e}")
|
73 |
-
doc.execution()
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
print(f"Total Memory: {psutil.virtual_memory().total / (1024**3):.2f} GB")
|
|
|
3 |
# python core libraries
|
4 |
import re
|
5 |
import psutil
|
6 |
+
import time
|
7 |
+
import random
|
8 |
# streamlit
|
9 |
import streamlit as st
|
10 |
# components from other authors
|
|
|
13 |
from audio_processing.A2T import A2T
|
14 |
from audio_processing.T2A import T2A
|
15 |
from llm.utils.chat import Conversation
|
16 |
+
from vlm.vlm import VLM
|
17 |
# utils modules
|
18 |
from utils.keywords import keywords
|
19 |
from utils.prompt_toggle import select_prompt, load_prompts
|
20 |
+
from utils.documentation import create_hand_gesture_doc
|
21 |
+
from utils.image_caption import ImageCaption
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
prompts = load_prompts()
|
|
|
24 |
chat = Conversation()
|
25 |
t2a = T2A()
|
26 |
+
vlm = VLM()
|
27 |
+
ic = ImageCaption()
|
28 |
|
29 |
def remove_labels_with_regex(text: str):
|
|
|
30 |
pattern = r'^(Human:|AI:|Chelsea:)\s*'
|
31 |
+
cleaned_text = re.sub(pattern, '', text, flags=re.MULTILINE)
|
32 |
return cleaned_text
|
33 |
|
34 |
+
def exctrator(sentence, phrase="show me your image"):
|
35 |
+
extracted_text = sentence.split(phrase)[1].strip() if phrase in sentence else ""
|
36 |
+
return extracted_text
|
37 |
+
|
38 |
+
def switching(text):
|
39 |
+
command = re.search("show me your image", text.lower(), re.IGNORECASE)
|
40 |
+
result = None
|
41 |
+
|
42 |
+
if command:
|
43 |
+
prompt = exctrator(text.lower())
|
44 |
+
# Завантажуємо зображення
|
45 |
+
uploaded_image = ic.load_image()
|
46 |
|
47 |
+
if uploaded_image is not None:
|
48 |
+
# Якщо зображення завантажено, виконуємо обробку
|
49 |
+
result = ic.send2ai(model=vlm, prompt=prompt)
|
50 |
+
else:
|
51 |
+
# Якщо зображення ще не завантажене, показуємо попередження
|
52 |
+
st.warning("No image uploaded yet. Please upload an image to continue.")
|
53 |
+
else:
|
54 |
+
prompt = select_prompt(input_text=text, prompts=prompts, keywords=keywords)
|
55 |
+
result = chat.chatting(prompt=prompt if prompt is not None else text)
|
56 |
|
57 |
+
print(f"Prompt:\n{prompt}")
|
58 |
+
prompt = None
|
59 |
+
return result
|
60 |
|
61 |
|
62 |
def main():
|
63 |
try:
|
64 |
mic = mic_recorder(start_prompt="Record", stop_prompt="Stop", just_once=True, use_container_width=True)
|
65 |
if mic is not None:
|
66 |
+
start_time = time.perf_counter()
|
67 |
a2t = A2T(mic["bytes"])
|
68 |
text = a2t.predict()
|
69 |
+
print(f"Text from A2T:\n{text}")
|
70 |
+
execution_time = time.perf_counter() - start_time
|
71 |
+
print(f"App.py -> main() -> time of execution A2T -> {execution_time}s")
|
72 |
|
73 |
+
output = switching(text)
|
74 |
+
response = remove_labels_with_regex(text=output)
|
75 |
+
start_time_t2a = time.perf_counter()
|
|
|
76 |
t2a.autoplay(response)
|
77 |
+
execution_time_t2a = time.perf_counter() - start_time_t2a
|
78 |
+
print(f"App.py -> main() -> time of execution T2A -> {execution_time_t2a}s")
|
79 |
+
print(ic.pil_image)
|
80 |
|
81 |
if response:
|
82 |
st.markdown(f"Your input: {text}")
|
83 |
st.markdown(f"Chelsea response: {response}")
|
84 |
|
85 |
+
if ic.pil_image is not None:
|
86 |
+
st.image(ic.pil_image, caption="Uploaded Image", use_column_width=True)
|
87 |
response = None
|
|
|
|
|
88 |
|
89 |
except Exception as e:
|
90 |
print(f"An error occurred in main finction, reasone is: {e}")
|
|
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
print(f"Total Memory: {psutil.virtual_memory().total / (1024**3):.2f} GB")
|