import gradio as gr
import requests
import json
import os
import sys
import subprocess
import time
import threading
from misc.misc import send_feature_suggestion_to_discord, get_system_usage
from tools.Ollama.ollamaai import summarize, unload_model, rewrite_text, convert_to_bullet_points
from tools.Ollama.ollama_utils_interface import ollama_utils_interface
# from tools.summarization import summarize
from tools.sdxl_fash import sdxl_flash_image, sdxl_examples, free_memory_sdxl_flash
from tools.reply import *
from tools.power import embed_power_website, format_and_display_power_data
from tools.remote_control import toggle_device, update_all_status
from tools.downloader import process_video, handle_url
from tools.whisper import mf_transcribe, file_transcribe, yt_transcribe
from tools.kahoot import start_bot, stop_bot, stop_all_bots, get_running_bots, generate_name, log_bot_action
from tools.SeeMoreDetails.app import seemoredetails_interface
print("imported SeeMoreDetails.app")
from tools.IllusionDiffusion.app1 import illusion_interface
print("imported IllusionDiffusion.app1")
from tools.BRIA.app import bria_interface
print("imported BRIA.app")
from tools.ormbg.app import ormbg_interface
print("imported ormbg.app")
from tools.stableaudio import stableaudio_interface, stableaudio_examples
print("imported stableaudio")
from tools.comfyui2.app9 import comfyui_interface
print("imported comfyui2.app9")
from tools.rag.l_copy10 import rag_interface
# Import pyngrok to use ngrok
from pyngrok import ngrok
def clear_gpu_memory():
import torch
import gc
if torch.cuda.is_available():
torch.cuda.empty_cache()
# free_memory_sdxl_flash()
# Force garbage collection
# unload_model("phi3:mini-128k")
gc.collect()
# Function to check VRAM usage
def check_vram_usage(threshold=5):
import pynvml
pynvml.nvmlInit()
handle = pynvml.nvmlDeviceGetHandleByIndex(0)
info = pynvml.nvmlDeviceGetMemoryInfo(handle)
vram_used_gb = info.used / 1024 ** 3 # Convert bytes to GB
return vram_used_gb
# Function to run in a separate thread to monitor VRAM usage
def monitor_vram_usage(interval=5, threshold=16):
while True:
vram_used_gb = check_vram_usage()
if vram_used_gb > threshold:
time.sleep(30)
gr.Info(f"VRAM usage: {vram_used_gb} / 24. Try to clear VRAM in the Misc tab to use other models!")
# clear_gpu_memory()
time.sleep(interval)
# Start the VRAM monitoring thread
monitor_thread = threading.Thread(target=monitor_vram_usage)
monitor_thread.daemon = True # Ensures the thread will close when the main program exits
monitor_thread.start()
with gr.Blocks(title="Seitz Suite", theme="soft") as demo:
with gr.Tab("Text Generation"):
with gr.Tab("General"):
rag_interface.render()
with gr.Tab("Utilities"):
ollama_utils_interface.render()
with gr.Tab("Reply"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("# Reply Generator")
reply_style = gr.Radio(choices=["auto", "funny", "formal", "casual"], label="Reply Style", value="auto")
input_text = gr.Textbox(placeholder="Enter text to generate a reply", lines=6)
reply_text = gr.Textbox(placeholder="Enter reply text")
generate_button = gr.Button("Generate Reply")
generated_reply = gr.Textbox(placeholder="Generated reply will appear here")
with gr.Column(scale=2):
gr.Markdown("# Fine-tune Response")
fine_tune_prompt = gr.Textbox(placeholder="Enter fine-tune prompt")
fine_tune_button = gr.Button("Fine-tune")
gr.Markdown("## Quick Fine-tune Options")
with gr.Row():
shorter_button = gr.Button("Shorter")
longer_button = gr.Button("Longer")
polite_button = gr.Button("More Polite")
direct_button = gr.Button("More Direct")
def update_fine_tune_text(text):
return text
generate_button.click(generate_reply, [reply_style, input_text, reply_text], generated_reply)
fine_tune_button.click(apply_fine_tune, [generated_reply, fine_tune_prompt], generated_reply)
shorter_button.click(make_shorter, generated_reply, generated_reply)
longer_button.click(make_longer, generated_reply, generated_reply)
polite_button.click(make_more_polite, generated_reply, generated_reply)
direct_button.click(make_more_direct, generated_reply, generated_reply)
# with gr.Tab("Summarize"):
# with gr.Row():
# with gr.Column():
# text = gr.Textbox(placeholder="Enter text here")
# with gr.Column():
# summary = gr.Textbox()
# gr.Button("Summarize").click(summarize, inputs=text, outputs=summary)
with gr.Tab("Image Generation"):
with gr.Tab("ComfyUI Image Generation"):
comfyui_interface.render()
with gr.Tab("See More Details"): # TODO make this pretty
gr.TabbedInterface([seemoredetails_interface], "See More Details")
with gr.Tab("Background Removal"):
# with gr.Tab("Bria"):
# bria_interface.render() # fix bria model loading
with gr.Tab("Ormbg"):
ormbg_interface.render()
with gr.Tab("Illusion Diffusion"):
gr.Markdown("# Not working right now!")
# illusion_interface.render()
with gr.Tab("Audio"):
with gr.Tab("Audio Transcription"):
gr.TabbedInterface([mf_transcribe, file_transcribe, yt_transcribe], ["Microphone", "Audio file", "YouTube"])
with gr.Tab("Stable Audio"):
stableaudio_interface.render()
with gr.Tab("Funny"):
with gr.Tab("Kahoot"):
gr.Markdown("## Kahoot Bot Manager")
with gr.Row():
with gr.Column():
game_id_input = gr.Textbox(label="Game ID")
nickname_input = gr.Textbox(label="Nickname")
theme_input = gr.Textbox(label="Quiz Theme", placeholder="Enter theme here")
start_button = gr.Button("Start Bot")
stop_button = gr.Button("Stop Bot")
def start_check_bot(game_id, nickname, theme):
if game_id == "" or game_id is None:
raise gr.Warning("Game ID cannot be empty.")
if len(nickname) > 15:
raise gr.Warning("Nickname must be less than 15 characters.")
if theme == "" or theme is None:
raise gr.Warning("Theme cannot be empty. Please fill a theme and try again.")
print(nickname)
if nickname == "" or nickname is None:
nickname = generate_name()
gr.Info(f"Generated nickname: {nickname}")
# start_bot(game_id, nickname, theme)
return start_bot(game_id, nickname, theme)
start_button.click(fn=start_check_bot, inputs=[game_id_input, nickname_input, theme_input], outputs=None)
stop_button.click(fn=stop_bot, inputs=[nickname_input], outputs=None)
with gr.Column():
num_bots_input = gr.Number(label="Number of Bots", precision=0)
start_multiple_button = gr.Button("Start Multiple Bots")
start_test_button = gr.Button("Start Test Bots (test1-4)")
stop_all_button = gr.Button("Stop All Bots")
def start_check_multiple_bots(game_id, num_bots, theme):
print(game_id, num_bots, theme)
if game_id == "" or game_id is None:
raise gr.Error("Game ID cannot be empty.")
if num_bots <= 0:
raise gr.Error("Number of bots must be greater than 0.")
if theme == "" or theme is None:
raise gr.Error("Theme cannot be empty. Please fill a theme and try again.")
for _ in range(int(num_bots)):
nickname = generate_name()
start_bot(game_id, nickname, theme)
return f"Started {num_bots} bots in game {game_id}."
def start_check_test_bots(game_id, theme):
if game_id == "" or game_id is None:
raise gr.Error("Game ID cannot be empty.")
if theme == "" or theme is None:
raise gr.Error("Theme cannot be empty. Please fill a theme and try again.")
test_bots = ["test1", "test2", "test3", "test4"]
for bot in test_bots:
start_bot(game_id, bot, theme)
return f"Started test bots (test1-4) in game {game_id}."
start_multiple_button.click(fn=start_check_multiple_bots, inputs=[game_id_input, num_bots_input, theme_input], outputs=None)
start_test_button.click(fn=start_check_test_bots, inputs=[game_id_input, theme_input], outputs=None)
stop_all_button.click(fn=stop_all_bots, inputs=None, outputs=None)
with gr.Row():
running_bots_display = gr.Dataframe(value=get_running_bots(), headers=["Nickname", "Game ID", "Status"], label="Running Bots", every=2)
bot_log = gr.TextArea(value="", label="Bot Actions Log", interactive=False)
def update_display():
bots = get_running_bots()
log = log_bot_action() # This will return the last 10 actions without adding a new one
if bots is None:
return gr.update(visible=False), gr.update(value=log)
return gr.update(value=bots, visible=True), gr.update(value=log)
running_bots_display.change(fn=update_display, inputs=None, outputs=[running_bots_display, bot_log], every=2)
with gr.Tab("Downloaders"):
with gr.Tab("Video Downloader"):
gr.Markdown("# Video Downloader")
gr.Markdown("Currently Supported:\n- YouTube Videos (youtube.com, youtu.be)")
url_input = gr.Textbox(label="Enter URL", placeholder="Enter the URL of the video")
with gr.Column(visible=False) as youtube_gui:
details = gr.Textbox(label="Video Details", interactive=False)
resolution_dropdown = gr.Dropdown(choices=[], label="Select Resolution", visible=False)
process_button = gr.Button("Process")
file_output = gr.File(label="Download File", visible=False, container=True)
status = gr.Textbox("Available to Download! Click Process to start download.", label="Status", visible=False)
def update_gui(url):
print("updating gui")
status_text, resolutions, url, gui_visible, process_visible, file_visible = handle_url(url)
print(status_text.encode('utf-8'))
youtube_gui.visible = gui_visible
return status_text, gr.update(choices=resolutions, value=resolutions[0] if resolutions else None, visible=bool(resolutions)), gui_visible, gr.update("Video Available to Download", visible=True), process_visible, file_visible
url_input.change(update_gui, inputs=url_input, outputs=[details, resolution_dropdown, youtube_gui, status, process_button, file_output])
process_button.click(
process_video,
inputs=[url_input, resolution_dropdown],
outputs=[status, file_output]
)
with gr.Tab("Managment"):
with gr.Tab("Shelly Control Panel"):
with gr.Tab(visible=False) as shelly_tab:
gr.Markdown("# Steckdose-Kicker Control Panel")
device_statuses = []
toggle_buttons = []
with open('devices.json') as f:
devices = json.load(f)
for device in devices:
with gr.Row():
gr.Markdown(device['name'])
device_status_output = gr.Textbox(label=f"{device['name']} Status", interactive=False)
device_button = gr.Button("Toggle")
device_button.click(fn=lambda name=device['name']: toggle_device(name), outputs=device_status_output)
device_statuses.append(device_status_output)
toggle_buttons.append(device_button)
get_all_status_button = gr.Button("Get All Status")
get_all_status_button.click(fn=update_all_status, outputs=device_statuses)
with gr.Row():
get_all_status_button
password_input = gr.Text("", label="Enter Password", visible=True)
login_button = gr.Button("Login", visible=True)
def check_password(password):
correct_password = "Banane,71" # Replace with the correct password
if password == correct_password:
# Password is correct, hide the components
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
else:
# Password is incorrect, keep the components visible
return None, gr.update(value=""), None
login_button.click(check_password, inputs=password_input, outputs=[password_input, login_button, shelly_tab])
with gr.Tab("Power Usage"):
with gr.Tab("live (only on same wifi)"):
gr.Markdown("## Website Embedder")
password_input = gr.Text("", label="Enter Password", visible=True)
embed_button = gr.Button("Login", visible=True)
website_embed = gr.HTML(label="Embedded Website", visible=True)
def fetch_website(password):
correct_password = "Banane,71" # Replace with the correct password
if password == correct_password:
# Password is correct, hide the components
return gr.update(visible=False), gr.update(visible=False), embed_power_website(password)
else:
# Password is incorrect, keep the components visible
return None, gr.update(value=""), None
embed_button.click(fetch_website, inputs=[password_input], outputs=[password_input, embed_button, website_embed])
with gr.Tab("data only"):
output_text = gr.HTML(label="Output Data")
def fetch_data_and_display():
return format_and_display_power_data()
fetch_button = gr.Button("Fetch Data")
fetch_button.click(fetch_data_and_display, inputs=[], outputs=output_text)
with gr.Tab("Embed Page"):
gr.Markdown("# Embed Page")
url_input = gr.Textbox("", label="Enter URL")
submit_button = gr.Button("Submit")
reset_button = gr.Button("Reset")
website_embed = gr.HTML("", label="test")
def embed_page(url):
if url:
html = f"<iframe src='{url}' width='100%' height='1000px'></iframe>"
return gr.update(value=html)
else:
return gr.update(value="")
def reset_embed():
return gr.update(value="")
submit_button.click(embed_page, inputs=url_input, outputs=website_embed)
reset_button.click(reset_embed, inputs=None, outputs=website_embed)
with gr.Tab("Suggest Feature"):
# Add an input field for feature suggestions
feature_suggestion = gr.Textbox(label="Feature Suggestion", placeholder="Enter your feature suggestion")
# Add a button to submit the feature suggestion
submit_button = gr.Button("Submit")
# Define the notification message
message = gr.Markdown(visible=False)
def handle_feature_suggestion(suggestion):
if suggestion == "":
return gr.update(value=""), gr.update(value="Please enter a valid feature suggestion", visible=True)
response = send_feature_suggestion_to_discord(suggestion)
return gr.update(value=""), gr.update(value=response, visible=True)
# Link the button to the feature suggestion function and clear input
submit_button.click(
fn=handle_feature_suggestion,
inputs=feature_suggestion,
outputs=[feature_suggestion, message]
)
with gr.Tab("Theo"):
with gr.Tab("Rewrite"):
gr.Markdown("# Text Rewriter")
input_text = gr.Textbox(label="Input Text", lines=5, placeholder="Enter the text you want to rewrite")
style_choice = gr.Radio(["Rewrite in my style", "Convert to bullet points"], label="Output Style", value="Rewrite in my style")
output_text = gr.Textbox(label="Rewritten Text", lines=5)
rewrite_button = gr.Button("Rewrite")
def process_text(text, style):
if style == "Rewrite in my style":
return rewrite_text(text)
else:
return convert_to_bullet_points(text)
rewrite_button.click(fn=process_text, inputs=[input_text, style_choice], outputs=output_text)
with gr.Tab("Misc"):
gr.Markdown("# Miscellaneous Functions")
# with gr.Group():
# gr.Markdown("## Restart GUI")
# gr.Markdown("This will restart the entire Gradio application. Any unsaved work will be lost.")
# restart_button = gr.Button("Restart GUI")
# restart_button.click(fn=restart_gui, inputs=None, outputs=None)
with gr.Group():
gr.Markdown("## Get GPU memory usage and other details like CPU and RAM usage")
cpu_output = gr.Textbox(label="CPU Usage")
ram_output = gr.Textbox(label="RAM Usage")
gpu_output = gr.Textbox(label="GPU Memory Usage")
gr.Button("Get Usage").click(get_system_usage, outputs=[cpu_output, ram_output, gpu_output])
with gr.Group():
gr.Markdown("## Clear GPU memory")
def clear_gpu_memory_():
import torch
import gc
# unload_model("phi3:mini-128k")
# free_memory_sdxl_flash()
# from numba import cuda
# device = cuda.get_current_device()
# device.reset()
print("test")
gc.collect()
torch.cuda.empty_cache()
clear_gpu_memory_button = gr.Button("Clear GPU memory")
clear_gpu_memory_button.click(fn=clear_gpu_memory, inputs=None, outputs=None)
clear_ollama_button = gr.Button("Clear Ollama memory")
clear_ollama_button.click(fn=unload_model, inputs=None, outputs=None)
with gr.Tab("About") as about:
gr.Markdown("""
<h2 style="color: #FFD700;">🌟 About 🌟</h2>
<p style="font-size: 16px; line-height: 1.6; color: #FFFFFF;">
Hi, I'm <b>Theo</b>, a <span style="color: #FFA500;">Python developer</span> currently working on this project.
I started this project on <i>May 25th</i> and am creating a nice website for me and my dad.
</p>
<p style="font-size: 16px; line-height: 1.6; color: #FFFFFF;">
🚀 This journey has been exciting, and I'm thrilled to share it with you! (Totally not AI generated :>)
</p>
""", elem_id="about-markdown")
gr.Image(value="images/image1.jpg", label="")
about.css = """
#about-markdown h2 {
text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.7);
}
#about-markdown p {
margin: 10px 0;
color: #FFFFFF;
}
"""
# Start the Gradio app without 'share=True' and 'block=False'
app, local_url, share_url = demo.queue(default_concurrency_limit=1).launch(
server_name="0.0.0.0", server_port=7860, share=False, block=False
)
# Start ngrok tunnel to the Gradio app port
tunnel = ngrok.connect(7860)
public_url = tunnel.public_url
print("Public ngrok URL:", public_url)
# Send the public ngrok URL to the Discord webhook
webhook_url = 'https://discord.com/api/webhooks/1292040223295012864/lirHrCeLLSAwgePfmAHSSCCkCmUv37VdYnpU_GkxPW6h6a2QhRzrTK1OhjTyc0hmYn41'
data = {'content': public_url}
response = requests.post(webhook_url, data=data)
if response.status_code == 204:
print("ngrok URL sent to Discord webhook successfully.")
else:
print(f"Failed to send ngrok URL to Discord webhook. Status code: {response.status_code}")
# Keep the script running
import signal
def signal_handler(sig, frame):
print('Shutting down ngrok tunnel...')
ngrok.disconnect(public_url)
ngrok.kill()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.pause()