Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,6 +10,9 @@ from dotenv import load_dotenv
|
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
12 |
|
|
|
|
|
|
|
13 |
# Logging setup
|
14 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s:%(message)s', handlers=[logging.StreamHandler()])
|
15 |
|
@@ -23,9 +26,9 @@ intents.guild_messages = True
|
|
23 |
# Inference API client setup
|
24 |
hf_client = InferenceClient("meta-llama/Meta-Llama-3.1-70B-Instruct", token=os.getenv("HF_TOKEN"))
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
|
30 |
# Specific channel ID
|
31 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
@@ -60,7 +63,7 @@ class MyClient(discord.Client):
|
|
60 |
# Extract English keywords from the meaning analysis
|
61 |
keywords = await extract_keywords(message)
|
62 |
if keywords:
|
63 |
-
# Search for high-resolution images using
|
64 |
image_urls = await search_images(keywords)
|
65 |
if image_urls:
|
66 |
# Create a thread with the requester and send high-resolution images
|
@@ -100,18 +103,17 @@ async def extract_keywords(message):
|
|
100 |
return keywords
|
101 |
|
102 |
async def search_images(keywords):
|
103 |
-
headers = {
|
104 |
-
"Authorization": f"Client-ID {UNSPLASH_ACCESS_KEY}"
|
105 |
-
}
|
106 |
params = {
|
107 |
-
"
|
108 |
-
"
|
|
|
|
|
109 |
}
|
110 |
-
response = requests.get(
|
111 |
if response.status_code == 200:
|
112 |
data = response.json()
|
113 |
-
return [
|
114 |
-
logging.error(f"
|
115 |
return None
|
116 |
|
117 |
async def create_thread_and_send_images(message, keywords, image_urls):
|
|
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
12 |
|
13 |
+
# Set Pixabay API key
|
14 |
+
os.environ['PIXABAY_API_KEY'] = "33492762-a28a596ec4f286f84cd328b17"
|
15 |
+
|
16 |
# Logging setup
|
17 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(name)s:%(message)s', handlers=[logging.StreamHandler()])
|
18 |
|
|
|
26 |
# Inference API client setup
|
27 |
hf_client = InferenceClient("meta-llama/Meta-Llama-3.1-70B-Instruct", token=os.getenv("HF_TOKEN"))
|
28 |
|
29 |
+
# Pixabay API setup
|
30 |
+
PIXABAY_API_KEY = os.getenv("PIXABAY_API_KEY")
|
31 |
+
PIXABAY_API_URL = "https://pixabay.com/api/"
|
32 |
|
33 |
# Specific channel ID
|
34 |
SPECIFIC_CHANNEL_ID = int(os.getenv("DISCORD_CHANNEL_ID"))
|
|
|
63 |
# Extract English keywords from the meaning analysis
|
64 |
keywords = await extract_keywords(message)
|
65 |
if keywords:
|
66 |
+
# Search for high-resolution images using Pixabay API
|
67 |
image_urls = await search_images(keywords)
|
68 |
if image_urls:
|
69 |
# Create a thread with the requester and send high-resolution images
|
|
|
103 |
return keywords
|
104 |
|
105 |
async def search_images(keywords):
|
|
|
|
|
|
|
106 |
params = {
|
107 |
+
"key": PIXABAY_API_KEY,
|
108 |
+
"q": keywords,
|
109 |
+
"per_page": 10, # Get up to 10 images
|
110 |
+
"image_type": "photo"
|
111 |
}
|
112 |
+
response = requests.get(PIXABAY_API_URL, params=params)
|
113 |
if response.status_code == 200:
|
114 |
data = response.json()
|
115 |
+
return [hit['largeImageURL'] for hit in data['hits']]
|
116 |
+
logging.error(f"Pixabay API error: {response.status_code}, {response.text}")
|
117 |
return None
|
118 |
|
119 |
async def create_thread_and_send_images(message, keywords, image_urls):
|