Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +119 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,121 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import base64
|
| 5 |
+
import io
|
| 6 |
+
from books import *
|
| 7 |
+
from streamlit_lottie import st_lottie, st_lottie_spinner
|
| 8 |
+
import json
|
| 9 |
+
import time
|
| 10 |
+
import streamlit.components.v1 as st1
|
| 11 |
+
from database_center import db_transaction
|
| 12 |
+
import os
|
| 13 |
+
from cloudhands import CloudHandsPayment
|
| 14 |
+
import uuid
|
| 15 |
|
| 16 |
+
|
| 17 |
+
payment_key=os.getenv['Payment_Key']
|
| 18 |
+
def load_local_lottie(path):
|
| 19 |
+
with open(path, 'r') as file:
|
| 20 |
+
return json.load(file)
|
| 21 |
+
|
| 22 |
+
def complete_payment():
|
| 23 |
+
if st.session_state.token :
|
| 24 |
+
chPay=st.session_state.chPay
|
| 25 |
+
try:
|
| 26 |
+
result = chPay.charge(
|
| 27 |
+
charge=0.5,
|
| 28 |
+
event_name="Sample cloudhands charge",
|
| 29 |
+
)
|
| 30 |
+
st.success(f"You payment is succeeded")
|
| 31 |
+
st.session_state.transaction_id=result.transaction_id
|
| 32 |
+
db_transaction.add({
|
| 33 |
+
'id':str(uuid.uuid4()),
|
| 34 |
+
'app':'app_title',
|
| 35 |
+
'transaction-id':result.transaction_id,
|
| 36 |
+
'price':0.5
|
| 37 |
+
|
| 38 |
+
})
|
| 39 |
+
except Exception as e:
|
| 40 |
+
st.error(f"Charge failed: {e}")
|
| 41 |
+
else:
|
| 42 |
+
st.error('Please generate your Tokens.')
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@st.dialog("Payment link")
|
| 46 |
+
def pay():
|
| 47 |
+
chPay = st.session_state.chPay
|
| 48 |
+
|
| 49 |
+
# Step 1: Show auth link only once
|
| 50 |
+
auth_url = chPay.get_authorization_url()
|
| 51 |
+
st.link_button("Authenticate", url=auth_url)
|
| 52 |
+
|
| 53 |
+
# Step 2: User pastes the code
|
| 54 |
+
code = st.text_input("Place your code")
|
| 55 |
+
|
| 56 |
+
if st.button("Exchange Code"):
|
| 57 |
+
try:
|
| 58 |
+
token = chPay.exchange_code_for_token(code)
|
| 59 |
+
st.session_state.token = token
|
| 60 |
+
st.success("Code exchanged successfully! Token stored.")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
st.error(f"Failed: {e}")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# ✅ Only initialize the state if not already set
|
| 66 |
+
if "loading_state" not in st.session_state:
|
| 67 |
+
st.session_state.loading_state = True
|
| 68 |
+
if "chPay" not in st.session_state:
|
| 69 |
+
st.session_state.chPay = CloudHandsPayment(
|
| 70 |
+
author_key=payment_key
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
if "token" not in st.session_state:
|
| 74 |
+
st.session_state.token = None
|
| 75 |
+
|
| 76 |
+
# ✅ Show loading animation only once at first load
|
| 77 |
+
if st.session_state.loading_state:
|
| 78 |
+
with st_lottie_spinner(load_local_lottie('Abstract Painting Loader.json'), key='hello'):
|
| 79 |
+
time.sleep(5)
|
| 80 |
+
st.session_state.loading_state = False
|
| 81 |
+
st.title("Old Book Image Generator")
|
| 82 |
+
st.sidebar.write("Please make your authenication token to use the model.")
|
| 83 |
+
authenication=st.sidebar.button('Authenicate')
|
| 84 |
+
if authenication:
|
| 85 |
+
pay()
|
| 86 |
+
caption = st.text_input("Enter an image caption:", "")
|
| 87 |
+
image_subject=st.pills("Select the subject of the image:", ["Narratives", "People", "Animals", "Buildings & monuments","Landscapes & places","Humor",""],selection_mode="single", default="People")
|
| 88 |
+
image_format=st.pills("Select the image format:", ["Landscape (wider)", "Portrait (taller)"], selection_mode="single", default="Portrait (taller)")
|
| 89 |
+
timeline=st.slider("Select the timeline:", 1800, 2000, 1803, step=1)
|
| 90 |
+
books=st.selectbox("Select a book title:",books, index=0)
|
| 91 |
+
book_title = books[books.index(books)]
|
| 92 |
+
if st.button("Generate Image") and caption:
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
with st_lottie_spinner(load_local_lottie('Circle brush.json'), key='loading'):
|
| 96 |
+
# Example: Using Replicate's Stable Diffusion API (replace with your own API key and endpoint)
|
| 97 |
+
payload = {
|
| 98 |
+
"image_description": caption,
|
| 99 |
+
"subject": image_subject,
|
| 100 |
+
"image_format": image_format,
|
| 101 |
+
"timeline": timeline,
|
| 102 |
+
"book_title": book_title
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
# Send POST request to your API
|
| 106 |
+
response = requests.post("https://8000-01k0vweebewdafymj6xwarn56n.cloudspaces.litng.ai/predict", json=payload)
|
| 107 |
+
|
| 108 |
+
# Check if request was successful
|
| 109 |
+
if response.status_code == 200:
|
| 110 |
+
complete_payment()
|
| 111 |
+
if st.session_state.transaction_id:
|
| 112 |
+
result = response.json()
|
| 113 |
+
|
| 114 |
+
# Decode the base64 image
|
| 115 |
+
image_data = base64.b64decode(result['output'])
|
| 116 |
+
|
| 117 |
+
# Convert to PIL image
|
| 118 |
+
image = Image.open(io.BytesIO(image_data))
|
| 119 |
+
st.image(image, caption=caption)
|
| 120 |
+
else:
|
| 121 |
+
st.error("Image generation failed.")
|