Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from diffusers import DiffusionPipeline
|
| 3 |
+
import torch
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
# Load the diffusion model on CPU
|
| 7 |
+
pipe = DiffusionPipeline.from_pretrained("tencent/SRPO")
|
| 8 |
+
pipe.to("cpu")
|
| 9 |
+
|
| 10 |
+
FIREBASE_API_KEY = "YOUR_FIREBASE_API_KEY"
|
| 11 |
+
|
| 12 |
+
def verify_id_token(id_token):
|
| 13 |
+
"""
|
| 14 |
+
Verify Firebase ID token using Firebase REST API
|
| 15 |
+
"""
|
| 16 |
+
url = f"https://identitytoolkit.googleapis.com/v1/accounts:lookup?key={FIREBASE_API_KEY}"
|
| 17 |
+
response = requests.post(url, json={"idToken": id_token})
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
return response.json()
|
| 20 |
+
else:
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
def generate_image_with_auth(prompt, id_token):
|
| 24 |
+
# Verify user
|
| 25 |
+
user_info = verify_id_token(id_token)
|
| 26 |
+
if not user_info:
|
| 27 |
+
return None, "❌ Authentication failed. Please log in with Google."
|
| 28 |
+
|
| 29 |
+
# Generate image on CPU
|
| 30 |
+
image = pipe(prompt, height=256, width=256, num_inference_steps=25).images[0]
|
| 31 |
+
return image, f"✅ Logged in as {user_info['users'][0]['email']}"
|
| 32 |
+
|
| 33 |
+
# Gradio interface with two outputs: image + login message
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("## SRPO Diffusion Generator (CPU) with Google Login")
|
| 36 |
+
with gr.Row():
|
| 37 |
+
prompt_input = gr.Textbox(label="Prompt")
|
| 38 |
+
id_token_input = gr.Textbox(label="Firebase ID Token (from Google login)", placeholder="Paste ID token here")
|
| 39 |
+
image_output = gr.Image(label="Generated Image")
|
| 40 |
+
login_status = gr.Textbox(label="Login Status")
|
| 41 |
+
generate_btn = gr.Button("Generate Image")
|
| 42 |
+
|
| 43 |
+
generate_btn.click(
|
| 44 |
+
fn=generate_image_with_auth,
|
| 45 |
+
inputs=[prompt_input, id_token_input],
|
| 46 |
+
outputs=[image_output, login_status]
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
demo.launch()
|