Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| from PIL import Image | |
| import base64 | |
| import io | |
| from books import * | |
| from streamlit_lottie import st_lottie, st_lottie_spinner | |
| import json | |
| import time | |
| import streamlit.components.v1 as st1 | |
| from database_center import db_transaction | |
| import os | |
| from cloudhands import CloudHandsPayment | |
| import uuid | |
| os.environ["STREAMLIT_CONFIG_DIR"] = os.path.join(os.getcwd(), ".streamlit") | |
| payment_key=os.getenv('Payment_Key') | |
| def load_local_lottie(path): | |
| with open(path, 'r') as file: | |
| return json.load(file) | |
| def complete_payment(): | |
| if st.session_state.token : | |
| chPay=st.session_state.chPay | |
| try: | |
| result = chPay.charge( | |
| charge=0.5, | |
| event_name="Sample cloudhands charge", | |
| ) | |
| st.success(f"You payment is succeeded") | |
| st.session_state.transaction_id=result.transaction_id | |
| db_transaction.add({ | |
| 'id':str(uuid.uuid4()), | |
| 'app':'app_title', | |
| 'transaction-id':result.transaction_id, | |
| 'price':0.5 | |
| }) | |
| except Exception as e: | |
| st.error(f"Charge failed: {e}") | |
| else: | |
| st.error('Please generate your Tokens.') | |
| def pay(): | |
| chPay = st.session_state.chPay | |
| # Step 1: Show auth link only once | |
| auth_url = chPay.get_authorization_url() | |
| st.link_button("Authenticate", url=auth_url) | |
| # Step 2: User pastes the code | |
| code = st.text_input("Place your code") | |
| if st.button("Exchange Code"): | |
| try: | |
| token = chPay.exchange_code_for_token(code) | |
| st.session_state.token = token | |
| st.success("Code exchanged successfully! Token stored.") | |
| except Exception as e: | |
| st.error(f"Failed: {e}") | |
| # β Only initialize the state if not already set | |
| if "loading_state" not in st.session_state: | |
| st.session_state.loading_state = True | |
| if "chPay" not in st.session_state: | |
| st.session_state.chPay = CloudHandsPayment( | |
| author_key=payment_key | |
| ) | |
| if "token" not in st.session_state: | |
| st.session_state.token = None | |
| # β Show loading animation only once at first load | |
| if st.session_state.loading_state: | |
| with st_lottie_spinner(load_local_lottie('./src/Abstract Painting Loader.json'), key='hello'): | |
| time.sleep(5) | |
| st.session_state.loading_state = False | |
| st.title("Old Book Image Generator") | |
| st.sidebar.write("Please make your authenication token to use the model.") | |
| authenication=st.sidebar.button('Authenicate') | |
| if authenication: | |
| pay() | |
| caption = st.text_input("Enter an image caption:", "") | |
| image_subject=st.pills("Select the subject of the image:", ["Narratives", "People", "Animals", "Buildings & monuments","Landscapes & places","Humor",""],selection_mode="single", default="People") | |
| image_format=st.pills("Select the image format:", ["Landscape (wider)", "Portrait (taller)"], selection_mode="single", default="Portrait (taller)") | |
| timeline=st.slider("Select the timeline:", 1800, 2000, 1803, step=1) | |
| books=st.selectbox("Select a book title:",books, index=0) | |
| book_title = books[books.index(books)] | |
| if st.button("Generate Image") and caption: | |
| with st_lottie_spinner(load_local_lottie('./src/Circle brush.json'), key='loading'): | |
| # Example: Using Replicate's Stable Diffusion API (replace with your own API key and endpoint) | |
| payload = { | |
| "image_description": caption, | |
| "subject": image_subject, | |
| "image_format": image_format, | |
| "timeline": timeline, | |
| "book_title": book_title | |
| } | |
| # Send POST request to your API | |
| response = requests.post("https://8000-01k0vweebewdafymj6xwarn56n.cloudspaces.litng.ai/predict", json=payload) | |
| # Check if request was successful | |
| if response.status_code == 200: | |
| complete_payment() | |
| if st.session_state.transaction_id: | |
| result = response.json() | |
| # Decode the base64 image | |
| image_data = base64.b64decode(result['output']) | |
| # Convert to PIL image | |
| image = Image.open(io.BytesIO(image_data)) | |
| st.image(image, caption=caption) | |
| else: | |
| st.error("Image generation failed.") | |