Spaces:
Runtime error
Runtime error
Sigrid De los Santos
commited on
Commit
·
9c57dcd
1
Parent(s):
9df4cc0
Remove remaining binary file for Hugging Face
Browse files- src/image_search.py +4 -6
- src/main.py +5 -1
src/image_search.py
CHANGED
|
@@ -4,11 +4,9 @@ from openai import OpenAI
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
load_dotenv()
|
| 7 |
-
|
| 8 |
UNSPLASH_ACCESS_KEY = os.getenv("UNSPLASH_ACCESS_KEY")
|
| 9 |
-
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 10 |
|
| 11 |
-
def extract_unsplash_keywords(title, num_keywords=2):
|
| 12 |
prompt = f"""
|
| 13 |
Extract {num_keywords} distinct, concise, visually-relevant keywords for Unsplash image search from this article title.
|
| 14 |
|
|
@@ -16,6 +14,7 @@ Format: space-separated, lowercase. No punctuation.
|
|
| 16 |
Title: {title}
|
| 17 |
"""
|
| 18 |
try:
|
|
|
|
| 19 |
response = client.chat.completions.create(
|
| 20 |
model="gpt-4",
|
| 21 |
messages=[{"role": "user", "content": prompt}],
|
|
@@ -26,8 +25,8 @@ Title: {title}
|
|
| 26 |
print(f"⚠️ Keyword extraction failed: {e}")
|
| 27 |
return title # fallback
|
| 28 |
|
| 29 |
-
def search_unsplash_image(title):
|
| 30 |
-
keywords = extract_unsplash_keywords(title)
|
| 31 |
try:
|
| 32 |
response = requests.get(
|
| 33 |
"https://api.unsplash.com/search/photos",
|
|
@@ -42,7 +41,6 @@ def search_unsplash_image(title):
|
|
| 42 |
author_username = photo["user"]["username"]
|
| 43 |
author_link = f"https://unsplash.com/@{author_username}"
|
| 44 |
|
| 45 |
-
# 💡 Return clean HTML credit
|
| 46 |
image_credit_html = (
|
| 47 |
f'Photo by <a href="{author_link}" target="_blank">{author_name}</a> '
|
| 48 |
f'on <a href="https://unsplash.com" target="_blank">Unsplash</a>'
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
load_dotenv()
|
|
|
|
| 7 |
UNSPLASH_ACCESS_KEY = os.getenv("UNSPLASH_ACCESS_KEY")
|
|
|
|
| 8 |
|
| 9 |
+
def extract_unsplash_keywords(title, openai_api_key, num_keywords=2):
|
| 10 |
prompt = f"""
|
| 11 |
Extract {num_keywords} distinct, concise, visually-relevant keywords for Unsplash image search from this article title.
|
| 12 |
|
|
|
|
| 14 |
Title: {title}
|
| 15 |
"""
|
| 16 |
try:
|
| 17 |
+
client = OpenAI(api_key=openai_api_key)
|
| 18 |
response = client.chat.completions.create(
|
| 19 |
model="gpt-4",
|
| 20 |
messages=[{"role": "user", "content": prompt}],
|
|
|
|
| 25 |
print(f"⚠️ Keyword extraction failed: {e}")
|
| 26 |
return title # fallback
|
| 27 |
|
| 28 |
+
def search_unsplash_image(title, openai_api_key):
|
| 29 |
+
keywords = extract_unsplash_keywords(title, openai_api_key)
|
| 30 |
try:
|
| 31 |
response = requests.get(
|
| 32 |
"https://api.unsplash.com/search/photos",
|
|
|
|
| 41 |
author_username = photo["user"]["username"]
|
| 42 |
author_link = f"https://unsplash.com/@{author_username}"
|
| 43 |
|
|
|
|
| 44 |
image_credit_html = (
|
| 45 |
f'Photo by <a href="{author_link}" target="_blank">{author_name}</a> '
|
| 46 |
f'on <a href="https://unsplash.com" target="_blank">Unsplash</a>'
|
src/main.py
CHANGED
|
@@ -57,7 +57,11 @@ def run_value_investing_analysis(csv_path):
|
|
| 57 |
continue
|
| 58 |
|
| 59 |
report_body = generate_value_investor_report(topic, news)
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
metrics_md = build_metrics_box(topic, len(news))
|
| 62 |
full_md = metrics_md + report_body
|
| 63 |
|
|
|
|
| 57 |
continue
|
| 58 |
|
| 59 |
report_body = generate_value_investor_report(topic, news)
|
| 60 |
+
from image_search import search_unsplash_image
|
| 61 |
+
|
| 62 |
+
# Later inside your loop
|
| 63 |
+
image_url, image_credit = search_unsplash_image(topic, os.getenv("OPENAI_API_KEY"))
|
| 64 |
+
|
| 65 |
metrics_md = build_metrics_box(topic, len(news))
|
| 66 |
full_md = metrics_md + report_body
|
| 67 |
|