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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import base64
|
| 3 |
+
import io
|
| 4 |
+
import random
|
| 5 |
+
import time
|
| 6 |
+
from typing import List
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import aiohttp
|
| 9 |
+
import asyncio
|
| 10 |
+
from streamlit_image_select import image_select
|
| 11 |
+
import requests
|
| 12 |
+
import streamlit as st
|
| 13 |
+
import requests
|
| 14 |
+
import zipfile
|
| 15 |
+
import io
|
| 16 |
+
import pandas as pd
|
| 17 |
+
from core import *
|
| 18 |
+
from utils import icon
|
| 19 |
+
from streamlit_image_select import image_select
|
| 20 |
+
from PIL import Image
|
| 21 |
+
import random
|
| 22 |
+
import time
|
| 23 |
+
import base64
|
| 24 |
+
from typing import List
|
| 25 |
+
import aiohttp
|
| 26 |
+
import asyncio
|
| 27 |
+
import plotly.express as px
|
| 28 |
+
from common import set_page_container_style
|
| 29 |
+
|
| 30 |
+
replicate_text = "NicheImage - Subnet 23 - Bittensor"
|
| 31 |
+
replicate_logo = "assets/NicheTensorTransparent.png"
|
| 32 |
+
replicate_link = "https://github.com/NicheTensor/NicheImage"
|
| 33 |
+
|
| 34 |
+
st.set_page_config(
|
| 35 |
+
page_title="NicheImage Generator", page_icon=replicate_logo, layout="wide"
|
| 36 |
+
)
|
| 37 |
+
set_page_container_style(
|
| 38 |
+
max_width=1100,
|
| 39 |
+
max_width_100_percent=True,
|
| 40 |
+
padding_top=0,
|
| 41 |
+
padding_right=10,
|
| 42 |
+
padding_left=5,
|
| 43 |
+
padding_bottom=10,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def fetch_GoJourney(task_id):
|
| 48 |
+
endpoint = "https://api.midjourneyapi.xyz/mj/v2/fetch"
|
| 49 |
+
data = {"task_id": task_id}
|
| 50 |
+
response = requests.post(endpoint, json=data)
|
| 51 |
+
return response.json()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def get_or_create_eventloop():
|
| 55 |
+
try:
|
| 56 |
+
return asyncio.get_event_loop()
|
| 57 |
+
except RuntimeError as ex:
|
| 58 |
+
if "There is no current event loop in thread" in str(ex):
|
| 59 |
+
loop = asyncio.new_event_loop()
|
| 60 |
+
asyncio.set_event_loop(loop)
|
| 61 |
+
return asyncio.get_event_loop()
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# UI configurations
|
| 65 |
+
st.markdown(
|
| 66 |
+
"""<style>
|
| 67 |
+
#root > div:nth-child(1) > div > div > div > div > section > div {padding-top: 2rem;}
|
| 68 |
+
</style>
|
| 69 |
+
|
| 70 |
+
""",
|
| 71 |
+
unsafe_allow_html=True,
|
| 72 |
+
)
|
| 73 |
+
css = """
|
| 74 |
+
<style>
|
| 75 |
+
section.main > div:has(~ footer ) {
|
| 76 |
+
padding-bottom: 5px;
|
| 77 |
+
}
|
| 78 |
+
</style>
|
| 79 |
+
"""
|
| 80 |
+
st.markdown(css, unsafe_allow_html=True)
|
| 81 |
+
|
| 82 |
+
# API Tokens and endpoints from `.streamlit/secrets.toml` file
|
| 83 |
+
API_TOKEN = st.secrets["API_TOKEN"]
|
| 84 |
+
# Placeholders for images and gallery
|
| 85 |
+
generated_images_placeholder = st.empty()
|
| 86 |
+
gallery_placeholder = st.empty()
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def configure_sidebar() -> None:
|
| 90 |
+
"""
|
| 91 |
+
Setup and display the sidebar elements.
|
| 92 |
+
|
| 93 |
+
This function configures the sidebar of the Streamlit application,
|
| 94 |
+
including the form for user inputs and the resources section.
|
| 95 |
+
"""
|
| 96 |
+
with st.sidebar:
|
| 97 |
+
st.image(replicate_logo, use_column_width=True)
|
| 98 |
+
with st.form("my_form"):
|
| 99 |
+
prompt = st.text_area(
|
| 100 |
+
":blue[**Enter prompt ✍🏾**]",
|
| 101 |
+
value="a beautiful flower under the sun --ar 16:9",
|
| 102 |
+
)
|
| 103 |
+
with st.expander(
|
| 104 |
+
"📚 Advanced",
|
| 105 |
+
expanded=False,
|
| 106 |
+
):
|
| 107 |
+
uid = st.text_input("Specify an UID", value="-1")
|
| 108 |
+
secret_key = st.text_input("Enter secret key", value="")
|
| 109 |
+
seed = st.text_input("Seed", value="-1")
|
| 110 |
+
# The Big Red "Submit" Button!
|
| 111 |
+
submitted = st.form_submit_button(
|
| 112 |
+
"Submit", type="primary", use_container_width=True
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
return (
|
| 116 |
+
submitted,
|
| 117 |
+
prompt,
|
| 118 |
+
uid,
|
| 119 |
+
secret_key,
|
| 120 |
+
seed,
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def main_midjourney(submitted, prompt, uid, secret_key, seed):
|
| 125 |
+
data = {
|
| 126 |
+
"key": "capricorn_feb",
|
| 127 |
+
"prompt": prompt,
|
| 128 |
+
"model_name": "GoJourney",
|
| 129 |
+
}
|
| 130 |
+
print(data)
|
| 131 |
+
if submitted:
|
| 132 |
+
with st.status(
|
| 133 |
+
"👩🏾🍳 Whipping up your words into art...", expanded=True
|
| 134 |
+
) as status:
|
| 135 |
+
try:
|
| 136 |
+
if submitted:
|
| 137 |
+
with generated_images_placeholder.container():
|
| 138 |
+
loop = get_or_create_eventloop()
|
| 139 |
+
asyncio.set_event_loop(loop)
|
| 140 |
+
output = requests.post(
|
| 141 |
+
"http://proxy_client_nicheimage.nichetensor.com:10003/generate", json=data
|
| 142 |
+
)
|
| 143 |
+
output = output.json()
|
| 144 |
+
print(output)
|
| 145 |
+
task_id = output["task_id"]
|
| 146 |
+
task_response = fetch_GoJourney(task_id)
|
| 147 |
+
task_status = task_response["status"]
|
| 148 |
+
if task_status == "failed":
|
| 149 |
+
status.update(label="Task failed", state="error")
|
| 150 |
+
return
|
| 151 |
+
while True:
|
| 152 |
+
task_response = fetch_GoJourney(task_id)
|
| 153 |
+
if task_response["status"] == "finished":
|
| 154 |
+
status.update(label="Task finished", state="complete")
|
| 155 |
+
img_url = task_response["task_result"]["image_url"]
|
| 156 |
+
st.image(
|
| 157 |
+
img_url, use_column_width=True, output_format="PNG"
|
| 158 |
+
)
|
| 159 |
+
st.json(task_response)
|
| 160 |
+
break
|
| 161 |
+
else:
|
| 162 |
+
status.update(
|
| 163 |
+
label=f"Task is still processing - {task_response['status']} - {task_response['meta']['task_request']['process_mode']}",
|
| 164 |
+
state="running",
|
| 165 |
+
)
|
| 166 |
+
time.sleep(2)
|
| 167 |
+
except Exception as e:
|
| 168 |
+
st.error(f"Error: {e}")
|
| 169 |
+
st.stop()
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def main():
|
| 173 |
+
"""
|
| 174 |
+
Main function to run the Streamlit application.
|
| 175 |
+
|
| 176 |
+
This function initializes the sidebar configuration and the main page layout.
|
| 177 |
+
It retrieves the user inputs from the sidebar, and passes them to the main page function.
|
| 178 |
+
The main page function then generates images based on these inputs.
|
| 179 |
+
"""
|
| 180 |
+
(
|
| 181 |
+
submitted,
|
| 182 |
+
prompt,
|
| 183 |
+
uid,
|
| 184 |
+
secret_key,
|
| 185 |
+
seed,
|
| 186 |
+
) = configure_sidebar()
|
| 187 |
+
main_midjourney(
|
| 188 |
+
submitted,
|
| 189 |
+
prompt,
|
| 190 |
+
uid,
|
| 191 |
+
secret_key,
|
| 192 |
+
seed,
|
| 193 |
+
)
|
| 194 |
+
if not submitted:
|
| 195 |
+
with generated_images_placeholder.container():
|
| 196 |
+
st.image(
|
| 197 |
+
"https://img.midjourneyapi.xyz/mj/a4a88dfe-4e68-4ff3-8ab1-85a4c2ee5792.png",
|
| 198 |
+
use_column_width=True,
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
if __name__ == "__main__":
|
| 203 |
+
main()
|
0_📊_Home_&_Statistics.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="NicheImage Studio", layout="wide")
|
| 7 |
+
st.markdown("## :black[Image Generation Studio by NicheImage]")
|
| 8 |
+
replicate_logo = "assets/NicheTensorTransparent.png"
|
| 9 |
+
|
| 10 |
+
with st.sidebar:
|
| 11 |
+
st.image(replicate_logo, use_column_width=True)
|
| 12 |
+
st.markdown(
|
| 13 |
+
"""
|
| 14 |
+
**NicheImage is a decentralized network of image generation models, powered by the Bittensor protocol. Below you find information about the current models on the network.**
|
| 15 |
+
""",
|
| 16 |
+
unsafe_allow_html=True,
|
| 17 |
+
)
|
| 18 |
+
response = requests.get(
|
| 19 |
+
"http://proxy_client_nicheimage.nichetensor.com:10003/get_uid_info"
|
| 20 |
+
)
|
| 21 |
+
if response.status_code == 200:
|
| 22 |
+
response = response.json()
|
| 23 |
+
# Plot distribution of models
|
| 24 |
+
model_distribution = {}
|
| 25 |
+
for uid, info in response["all_uid_info"].items():
|
| 26 |
+
model_name = info["model_name"]
|
| 27 |
+
model_distribution[model_name] = model_distribution.get(model_name, 0) + 1
|
| 28 |
+
fig = px.pie(
|
| 29 |
+
values=list(model_distribution.values()),
|
| 30 |
+
names=list(model_distribution.keys()),
|
| 31 |
+
title="Model Distribution",
|
| 32 |
+
)
|
| 33 |
+
st.plotly_chart(fig)
|
| 34 |
+
transformed_dict = []
|
| 35 |
+
for k, v in response["all_uid_info"].items():
|
| 36 |
+
transformed_dict.append(
|
| 37 |
+
{
|
| 38 |
+
"uid": k,
|
| 39 |
+
"model_name": v["model_name"],
|
| 40 |
+
"mean_score": (
|
| 41 |
+
sum(v["scores"]) / (len(v["scores"])) if len(v["scores"]) > 0 else 0
|
| 42 |
+
),
|
| 43 |
+
}
|
| 44 |
+
)
|
| 45 |
+
transformed_dict = pd.DataFrame(transformed_dict)
|
| 46 |
+
# plot N bar chart for N models, sorted by mean score
|
| 47 |
+
for model in model_distribution.keys():
|
| 48 |
+
model_data = transformed_dict[transformed_dict["model_name"] == model]
|
| 49 |
+
model_data = model_data.sort_values(by="mean_score", ascending=False)
|
| 50 |
+
if model_data.mean_score.sum() == 0:
|
| 51 |
+
continue
|
| 52 |
+
st.write(f"Model: {model}")
|
| 53 |
+
st.bar_chart(model_data[["uid", "mean_score"]].set_index("uid"))
|
| 54 |
+
|
| 55 |
+
else:
|
| 56 |
+
st.error("Error getting miner info")
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
replicate
|
| 2 |
+
streamlit==1.29
|
| 3 |
+
requests
|
| 4 |
+
streamlit-image-select
|
| 5 |
+
plotly
|
| 6 |
+
pandas
|
| 7 |
+
httpx
|
| 8 |
+
aiohttp
|