Spaces:
Sleeping
Sleeping
Mahdi Naser Moghadasi commited on
Commit Β·
bdef6ae
1
Parent(s): 3b0fe6a
Add Hugging Face image generation using Stable Diffusion models
Browse files
app.py
CHANGED
|
@@ -1385,10 +1385,87 @@ def call_hugging_face_api_content(prompt):
|
|
| 1385 |
print(f"β Content API error: {str(e)}")
|
| 1386 |
return None
|
| 1387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1388 |
def get_educational_image_url(topic, description, content_type):
|
| 1389 |
-
"""Get educational image URL using
|
| 1390 |
try:
|
| 1391 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1392 |
educational_sources = [
|
| 1393 |
# 1. Lorem Picsum (reliable placeholder)
|
| 1394 |
f"https://picsum.photos/800/600?random={hash(topic) % 1000}",
|
|
@@ -1400,13 +1477,13 @@ def get_educational_image_url(topic, description, content_type):
|
|
| 1400 |
f"https://via.placeholder.com/800x600/4f46e5/ffffff?text={content_type.replace(' ', '+')}+{topic.replace(' ', '+')}",
|
| 1401 |
]
|
| 1402 |
|
| 1403 |
-
# Try each source
|
| 1404 |
for i, url in enumerate(educational_sources):
|
| 1405 |
try:
|
| 1406 |
-
print(f"π Trying
|
| 1407 |
response = requests.head(url, timeout=5)
|
| 1408 |
if response.status_code == 200:
|
| 1409 |
-
print(f"β
Found working image: {url}")
|
| 1410 |
return url
|
| 1411 |
except:
|
| 1412 |
continue
|
|
|
|
| 1385 |
print(f"β Content API error: {str(e)}")
|
| 1386 |
return None
|
| 1387 |
|
| 1388 |
+
def generate_image_with_huggingface(prompt, topic, content_type):
|
| 1389 |
+
"""Generate image using Hugging Face text-to-image models"""
|
| 1390 |
+
try:
|
| 1391 |
+
if not HF_TOKEN:
|
| 1392 |
+
print("β No HF_TOKEN available for image generation")
|
| 1393 |
+
return None
|
| 1394 |
+
|
| 1395 |
+
# Use Hugging Face Inference API for image generation
|
| 1396 |
+
headers = {
|
| 1397 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 1398 |
+
"Content-Type": "application/json"
|
| 1399 |
+
}
|
| 1400 |
+
|
| 1401 |
+
# Try different image generation models
|
| 1402 |
+
models = [
|
| 1403 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 1404 |
+
"runwayml/stable-diffusion-v1-5",
|
| 1405 |
+
"CompVis/stable-diffusion-v1-4"
|
| 1406 |
+
]
|
| 1407 |
+
|
| 1408 |
+
for model in models:
|
| 1409 |
+
try:
|
| 1410 |
+
print(f"π¨ Trying image generation with {model}")
|
| 1411 |
+
|
| 1412 |
+
data = {
|
| 1413 |
+
"inputs": prompt,
|
| 1414 |
+
"parameters": {
|
| 1415 |
+
"num_inference_steps": 20,
|
| 1416 |
+
"guidance_scale": 7.5,
|
| 1417 |
+
"width": 512,
|
| 1418 |
+
"height": 512
|
| 1419 |
+
}
|
| 1420 |
+
}
|
| 1421 |
+
|
| 1422 |
+
response = requests.post(
|
| 1423 |
+
f"https://api-inference.huggingface.co/models/{model}",
|
| 1424 |
+
headers=headers,
|
| 1425 |
+
json=data,
|
| 1426 |
+
timeout=30
|
| 1427 |
+
)
|
| 1428 |
+
|
| 1429 |
+
if response.status_code == 200:
|
| 1430 |
+
# Convert to base64 data URL
|
| 1431 |
+
import base64
|
| 1432 |
+
image_base64 = base64.b64encode(response.content).decode('utf-8')
|
| 1433 |
+
data_url = f"data:image/png;base64,{image_base64}"
|
| 1434 |
+
|
| 1435 |
+
print(f"β
Generated image with {model}")
|
| 1436 |
+
return data_url
|
| 1437 |
+
else:
|
| 1438 |
+
print(f"β {model} failed: {response.status_code}")
|
| 1439 |
+
continue
|
| 1440 |
+
|
| 1441 |
+
except Exception as e:
|
| 1442 |
+
print(f"β {model} error: {str(e)}")
|
| 1443 |
+
continue
|
| 1444 |
+
|
| 1445 |
+
return None
|
| 1446 |
+
|
| 1447 |
+
except Exception as e:
|
| 1448 |
+
print(f"β Image generation error: {str(e)}")
|
| 1449 |
+
return None
|
| 1450 |
+
|
| 1451 |
def get_educational_image_url(topic, description, content_type):
|
| 1452 |
+
"""Get educational image URL using Hugging Face generation first, then fallbacks"""
|
| 1453 |
try:
|
| 1454 |
+
# Create a detailed prompt for image generation
|
| 1455 |
+
image_prompt = f"Educational illustration of {topic}, {description}, {content_type}, clean diagram, colorful, professional, suitable for middle school students"
|
| 1456 |
+
|
| 1457 |
+
print(f"π¨ Generating image with prompt: {image_prompt}")
|
| 1458 |
+
|
| 1459 |
+
# Try to generate image with Hugging Face first
|
| 1460 |
+
generated_image = generate_image_with_huggingface(image_prompt, topic, content_type)
|
| 1461 |
+
|
| 1462 |
+
if generated_image:
|
| 1463 |
+
print(f"β
Generated image successfully")
|
| 1464 |
+
return generated_image
|
| 1465 |
+
|
| 1466 |
+
# Fallback to reliable sources if generation fails
|
| 1467 |
+
print("π Image generation failed, using fallback sources")
|
| 1468 |
+
|
| 1469 |
educational_sources = [
|
| 1470 |
# 1. Lorem Picsum (reliable placeholder)
|
| 1471 |
f"https://picsum.photos/800/600?random={hash(topic) % 1000}",
|
|
|
|
| 1477 |
f"https://via.placeholder.com/800x600/4f46e5/ffffff?text={content_type.replace(' ', '+')}+{topic.replace(' ', '+')}",
|
| 1478 |
]
|
| 1479 |
|
| 1480 |
+
# Try each fallback source
|
| 1481 |
for i, url in enumerate(educational_sources):
|
| 1482 |
try:
|
| 1483 |
+
print(f"π Trying fallback source {i+1}: {url}")
|
| 1484 |
response = requests.head(url, timeout=5)
|
| 1485 |
if response.status_code == 200:
|
| 1486 |
+
print(f"β
Found working fallback image: {url}")
|
| 1487 |
return url
|
| 1488 |
except:
|
| 1489 |
continue
|