Spaces:
Sleeping
Sleeping
from colorama import Style, Fore | |
import sys | |
import gradio as gr | |
import tty | |
import termios | |
def generate_progress_html(percent): | |
return f""" | |
<div class="progress-bar" style="width: 100%; background-color: #e0e0e0; border-radius: 10px; overflow: hidden; position: relative;"> | |
<div class="progress-bar-inner" style="height: 20px; background: linear-gradient(90deg, #76c7c0, #4ca1af); width: {percent}%; border-radius: 10px; transition: width 0.3s ease;"> | |
<div class="progress-text" style="position: absolute; width: 100%; top: 0; left: 0; height: 100%; display: flex; align-items: center; justify-content: center; color: #fff; font-weight: bold;">{percent}%</div> | |
</div> | |
</div> | |
""" | |
def get_char(): | |
"""Read a single character from standard input without echo.""" | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(sys.stdin.fileno()) | |
char = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return char | |
def get_multiline_input(): | |
"""Ask the human user_input till they press Ctrl+B.""" | |
print(Fore.BLUE + 'Enter your multiline text and press Ctrl+B when you are done:') | |
user_input = [] | |
current_line = [] | |
line_num = 1 | |
print(f"{line_num}> ", end='', flush=True) | |
while True: | |
char = get_char() | |
if ord(char) == 2: # Ctrl+B | |
break | |
elif char == '\r' or char == '\n': # Enter key | |
line = ''.join(current_line) | |
user_input.append(line) | |
current_line = [] | |
line_num += 1 | |
print(f"\n{line_num}> ", end='', flush=True) | |
elif char == '\x7f': # Backspace | |
if current_line: | |
current_line.pop() | |
# Move cursor back, overwrite with space, move back again | |
print('\b \b', end='', flush=True) | |
else: | |
current_line.append(char) | |
print(char, end='', flush=True) | |
print('\n\n') | |
return '\n'.join(user_input) | |
def system_update(update_message): | |
print(Style.RESET_ALL+ Fore.YELLOW + f"{update_message}") | |
def system_sub_update(update_message): | |
print(Style.RESET_ALL+ Fore.CYAN + f"{update_message}") | |
def system_output(update_message): | |
print(Style.RESET_ALL+ Fore.GREEN + + f"{update_message}") | |
def system_error(update_message): | |
print(Style.RESET_ALL + Fore.RED + f"{update_message}") |