Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
arsen3d committed Dec 22, 2024
1 parent 175e0dd commit 11dde92
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 29 deletions.
6 changes: 5 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
{
"label": "ensure-env-file",
"type": "shell",
"command": "touch ${workspaceFolder}/examples/gradio/.env",
"command": "touch ${workspaceFolder}/examples/gradio/.env && \n\
if [ ! -f ${workspaceFolder}/examples/gradio/.env ] || \n\
! grep -q \"WEB3_PRIVATE_KEY\" ${workspaceFolder}/examples/gradio/.env; then \n\
echo \"WEB3_PRIVATE_KEY=$(openssl rand -hex 32)\" >> ${workspaceFolder}/examples/gradio/.env; \n\
fi",
"presentation": {
"reveal": "silent"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/gradio/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ WORKDIR /app
# Install the dependencies
RUN pip install uv
RUN uv pip install gradio --system
RUN uv pip install watchdog --system
RUN uv pip install watchdog web3 --system
# Copy the rest of the application code into the container
# COPY . .

Expand Down
53 changes: 45 additions & 8 deletions examples/gradio/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,37 @@
import requests
import tempfile
import tarfile
from web3 import Web3

WEB3_PRIVATE_KEY = os.getenv("WEB3_PRIVATE_KEY", "")

# Connect to the RPC endpoint
web3 = Web3(Web3.HTTPProvider('https://demonet-chain-http.lilypad.tech'))
account = web3.eth.account.from_key(WEB3_PRIVATE_KEY)
wallet_address = account.address
# ERC20 contract address and ABI
contract_address = '0xa513E6E4b8f2a923D98304ec87F64353C4D5C853'
contract_abi = [
{
"constant": True,
"inputs": [{"name": "_owner", "type": "address"}],
"name": "balanceOf",
"outputs": [{"name": "balance", "type": "uint256"}],
"type": "function",
}
]

# Create contract instance
contract = web3.eth.contract(address=contract_address, abi=contract_abi)


def get_balance(wallet_address):
try:
balance_wei = contract.functions.balanceOf(wallet_address).call()
balance_eth = Web3.from_wei(balance_wei, 'ether')
return f"{balance_eth:.5f} LP"
except Exception as e:
return f"Error: {str(e)}"

head = f"""
<script src="/gradio_api/file=app/app.js"></script>
Expand Down Expand Up @@ -66,13 +95,16 @@ def run(dropdown,prompt,progress=gr.Progress()):

with gr.Blocks(head=head) as demo:
progress = gr.Progress()
# with gr.Blocks():
# with gr.Row():
# with gr.Column():
# gr.Label("Select a module to run")
# with gr.Column():
# gr.Label("Select a module to run")
# gr.Image(value="app/gradio.svg", type="pil"),
with gr.Blocks():
with gr.Row():
with gr.Column(scale=1):
gr.Label( label="LP Balance", value=get_balance(wallet_address))
with gr.Column(scale=1):
balance_wei = web3.eth.get_balance(wallet_address)
balance_eth = Web3.from_wei(balance_wei, 'ether')
gr.Label(label="ETH Balance", value=f"{balance_eth:.5f} ETH")
with gr.Column(scale=5):
gr.Label(label="Wallet Address", value=wallet_address)
dropdown = gr.Dropdown([
("Cowsay","cowsay:v0.0.4,Message"),
("SDXL","github.com/noryev/module-sdxl-ipfs:ae17e969cadab1c53d7cabab1927bb403f02fd2a,prompt"),
Expand All @@ -81,7 +113,7 @@ def run(dropdown,prompt,progress=gr.Progress()):
# value="Message",
interactive=True
)
inp = gr.Textbox(label="Input",placeholder="Prompt!!!")
inp = gr.Textbox(label="Input", placeholder="Enter wallet address")
run_button = gr.Button("Run")
image = gr.Image( visible=False)
file = gr.File( visible=False)
Expand All @@ -95,6 +127,11 @@ def run(dropdown,prompt,progress=gr.Progress()):
elem_classes=["monospace"],
show_copy_button=True)
err = gr.Textbox(label="Error", visible=False,)

def on_run_click(wallet_address):
balance = get_balance(wallet_address)
return balance

run_button.click(
show_api=False,
fn=lambda: (gr.Button(interactive=False), gr.Textbox(visible=False), gr.Textbox(visible=False), gr.File(visible=False),gr.Image(visible=False)), # Disable button first
Expand Down
38 changes: 19 additions & 19 deletions examples/gradio/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
services:
init:
image: alpine
command: >
/bin/sh -c '
set -ex &&
apk add --no-cache openssl bash &&
cd /app &&
if [ ! -f .env ] || ! grep -q "WEB3_PRIVATE_KEY" .env; then
echo "WEB3_PRIVATE_KEY=$(openssl rand -hex 32)" >> .env;
fi
'
volumes:
- .:/app
# init:
# image: alpine
# command: >
# /bin/sh -c '
# set -ex &&
# apk add --no-cache openssl bash &&
# cd /app &&
# if [ ! -f .env ] || ! grep -q "WEB3_PRIVATE_KEY" .env; then
# echo "WEB3_PRIVATE_KEY=$(openssl rand -hex 32)" >> .env;
# fi
# '
# volumes:
# - .:/app

gradio:
image: gradio
Expand All @@ -25,15 +25,15 @@ services:
- "7860:7860"
env_file:
- .env
depends_on:
init:
condition: service_completed_successfully
# depends_on:
# init:
# condition: service_completed_successfully

cli-wrapper:
image: cli-wrapper
build:
context: ../../
dockerfile: Dockerfile
depends_on:
init:
condition: service_completed_successfully
# depends_on:
# init:
# condition: service_completed_successfully

0 comments on commit 11dde92

Please sign in to comment.