Frames / app.py
KamiyabAli's picture
Update app.py
7428476 verified
import requests
import gradio as gr
from PIL import Image
import io
# Define the function to interact with the model, which will use the user's token
def query(text_input, user_token):
# Construct the headers with the user's token
headers = {"Authorization": f"Bearer {user_token}"}
# Construct the payload for the model
payload = {"inputs": text_input}
try:
# Make the POST request to the API with the user's token
response = requests.post(API_URL, headers=headers, json=payload)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Process the image if the request was successful
image_bytes = response.content
image = Image.open(io.BytesIO(image_bytes))
return image
else:
# Return an error message if the API call failed
return f"Error: {response.status_code} - {response.text}"
except Exception as e:
# Catch any unexpected errors and return the error message
return f"An error occurred: {str(e)}"
# Define the API URL
API_URL = "https://api-inference.huggingface.co/models/KamiyabAli/frames"
examples = [
["FRM$ a majestic mountain range with snow-capped peaks in the background, illuminated by the setting sun. The sky is a beautiful mix of oranges, pinks, and purples, creating a stunning backdrop for the majestic peaks."],
["FRM$ a realistic mountain landscape during the day, captured from a camera. A winding river flows through a lush green valley, with only one towering mountain in the distance covered in snow. The rest of the landscape is green and vibrant, contrasting with the white peak. The sky is partly cloudy, with soft, rolling clouds casting shadows over the terrain. Sunlight filters through the clouds, creating dynamic lighting across the valley, while the river glistens in the daylight. The snow-covered mountain stands as a striking focal point against the serene, natural surroundings."],
["FRM$ a river flowing near a mountain and plains land, firewatch style, watch tower on mountain, purplish dominant color, animals."]
]
# Create the Gradio interface
demo = gr.Interface(
fn=query,
inputs=[gr.Textbox(label="Text Input (e.g., 'Astronaut riding a horse')"),
gr.Textbox(label="Hugging Face Token (Enter Your Token Here)", type="password")],
outputs=gr.Image(type="pil", label="Generated Image"),
examples=examples
)
# Launch the Gradio interface
demo.launch(share=True)