Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import plotly.express as px
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
'Count': [10, 12, 5, 8]}
|
| 14 |
-
df = pd.DataFrame(data)
|
| 15 |
-
fig = px.bar(df, x='Animals', y='Count', title='Animal Count')
|
| 16 |
-
elif topic.lower() == 'fruits':
|
| 17 |
-
data = {'Fruit': ['Apple', 'Banana', 'Cherry', 'Date'],
|
| 18 |
-
'Count': [20, 15, 30, 10]}
|
| 19 |
-
df = pd.DataFrame(data)
|
| 20 |
-
fig = px.pie(df, names='Fruit', values='Count', title='Fruit Distribution')
|
| 21 |
-
else:
|
| 22 |
-
# Default graph if no matching topic
|
| 23 |
-
data = {'Topic': ['A', 'B', 'C', 'D'],
|
| 24 |
-
'Value': [25, 40, 15, 20]}
|
| 25 |
-
df = pd.DataFrame(data)
|
| 26 |
-
fig = px.line(df, x='Topic', y='Value', title='Example Line Graph')
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Build the Gradio interface
|
| 31 |
interface = gr.Interface(
|
| 32 |
-
fn=
|
| 33 |
-
inputs=gr.Textbox(label="Enter Topic (e.g.,
|
| 34 |
-
outputs=gr.
|
| 35 |
live=True,
|
| 36 |
-
title="Children's School Project
|
| 37 |
-
description="Enter a topic and
|
| 38 |
)
|
| 39 |
|
| 40 |
# Launch the app
|
| 41 |
interface.launch(share=True)
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import os
|
|
|
|
| 6 |
|
| 7 |
+
# Hugging Face API endpoint and model (Stable Diffusion in this case)
|
| 8 |
+
HF_MODEL_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
|
| 9 |
+
|
| 10 |
+
# Get API key securely from environment variable
|
| 11 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Function to generate image based on topic
|
| 14 |
+
def generate_image(topic):
|
| 15 |
+
# Prepare the payload for the Hugging Face API
|
| 16 |
+
payload = {
|
| 17 |
+
"inputs": topic,
|
| 18 |
+
"options": {"use_gpu": True}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
| 22 |
+
|
| 23 |
+
# Make the API call to Hugging Face
|
| 24 |
+
response = requests.post(HF_MODEL_URL, headers=headers, json=payload)
|
| 25 |
+
|
| 26 |
+
# Check if the request was successful
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
# Convert the response content (image) into a PIL Image object
|
| 29 |
+
image = Image.open(BytesIO(response.content))
|
| 30 |
+
return image
|
| 31 |
+
else:
|
| 32 |
+
return "Error generating image. Please try again."
|
| 33 |
|
| 34 |
# Build the Gradio interface
|
| 35 |
interface = gr.Interface(
|
| 36 |
+
fn=generate_image,
|
| 37 |
+
inputs=gr.Textbox(label="Enter Topic (e.g., Dog, Space, Nature)"),
|
| 38 |
+
outputs=gr.Image(),
|
| 39 |
live=True,
|
| 40 |
+
title="Children's School Project Image Generator",
|
| 41 |
+
description="Enter a topic and generate an image related to it for school projects. Example topics: Dog, Space, Nature."
|
| 42 |
)
|
| 43 |
|
| 44 |
# Launch the app
|
| 45 |
interface.launch(share=True)
|
| 46 |
+
|