Spaces:
Sleeping
Sleeping
""" | |
Streamlit web interface for the Logo Downloader | |
""" | |
import os | |
import logging | |
import streamlit as st | |
from pathlib import Path | |
from typing import Optional | |
from services.logo_downloader import LogoDownloader | |
from services.appconfig import GEMINI_API_KEY, DEFAULT_LOGOS_PER_ENTITY, MAX_LOGOS_PER_ENTITY | |
# Setup logging | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger(__name__) | |
def process_text_request(text: str, api_key: Optional[str], num_logos: int = DEFAULT_LOGOS_PER_ENTITY): | |
""" | |
Process text and download logos through Streamlit interface | |
""" | |
try: | |
if not text or not text.strip(): | |
return "β Please provide some text to analyze.", None, "No text provided." | |
if num_logos < 1 or num_logos > MAX_LOGOS_PER_ENTITY: | |
return f"β Number of logos must be between 1 and {MAX_LOGOS_PER_ENTITY}.", None, f"Invalid number: {num_logos}" | |
final_api_key = api_key.strip() if api_key and api_key.strip() else GEMINI_API_KEY | |
downloader = LogoDownloader(gemini_api_key=final_api_key) | |
results = downloader.process_text(text, num_logos) | |
if results['status'] == 'success' and results['stats']['total_downloads'] > 0: | |
status_msg = f"β {downloader.get_stats_summary()}" | |
zip_path = results.get('zip_path') | |
detailed_results = _format_detailed_results(results) | |
return status_msg, zip_path, detailed_results | |
elif results['status'] == 'warning': | |
return f"β οΈ {results['message']}", None, results.get('message', 'No details available') | |
else: | |
return f"β Processing failed: {results['message']}", None, results.get('message', 'Unknown error') | |
except Exception as e: | |
logger.error(f"Error in process_text_request: {e}") | |
return f"β An error occurred: {str(e)}", None, f"Error details: {str(e)}" | |
def _format_detailed_results(results): | |
if not results.get('results'): | |
return "No detailed results available." | |
details = [] | |
details.append(f"π **Processing Summary:**") | |
details.append(f"- Total entities found: {results['stats']['total_entities']}") | |
details.append(f"- Total logos downloaded: {results['stats']['total_downloads']}") | |
details.append(f"- Successful entities: {results['stats']['successful_entities']}") | |
details.append(f"- Failed entities: {results['stats']['failed_entities']}") | |
details.append("") | |
details.append("π **Entity Details:**") | |
for result in results['results']: | |
entity = result['entity'] | |
count = result['downloaded_count'] | |
if count > 0: | |
details.append(f"β **{entity}**: {count} logos downloaded") | |
else: | |
error_msg = result.get('error', 'No logos found') | |
details.append(f"β **{entity}**: Failed ({error_msg})") | |
return "\n".join(details) | |
def main(): | |
st.set_page_config(page_title="π¨ Logo Downloader", layout="centered") | |
st.title("π¨ Logo Downloader") | |
st.markdown("Extract entities from text and download their logos automatically.") | |
with st.form(key="logo_form"): | |
text_input = st.text_area( | |
"π Enter text containing company names, products, or brands:", | |
placeholder="e.g., We use AWS, Docker, React, and Adobe Photoshop for our projects", | |
height=150 | |
) | |
api_key_input = st.text_input( | |
"π Gemini API Key (optional)", | |
type="password", | |
placeholder="Enter your Gemini API key for enhanced extraction" | |
) | |
num_logos_input = st.slider( | |
"πΌοΈ Logos per entity", | |
min_value=1, | |
max_value=MAX_LOGOS_PER_ENTITY, | |
value=DEFAULT_LOGOS_PER_ENTITY, | |
step=1 | |
) | |
submit_btn = st.form_submit_button("π Download Logos") | |
if submit_btn: | |
with st.spinner("Processing logos..."): | |
status_msg, zip_path, detailed_results = process_text_request( | |
text_input, | |
api_key_input, | |
num_logos_input | |
) | |
st.markdown(status_msg) | |
if zip_path and Path(zip_path).exists(): | |
with open(zip_path, "rb") as f: | |
st.download_button( | |
label="π₯ Download Logos ZIP", | |
data=f, | |
file_name=Path(zip_path).name, | |
mime="application/zip" | |
) | |
st.markdown("## π Detailed Results") | |
st.markdown(detailed_results) | |
st.markdown("---") | |
st.info("π‘ Tip: Get a free Gemini API key at [Google AI Studio](https://makersuite.google.com/app/apikey) for better extraction accuracy.") | |
st.markdown("## π‘ Examples") | |
examples = [ | |
"Our tech stack includes React, Node.js, MongoDB, Docker, AWS, and we use Figma for design, along with GitHub for version control.", | |
"The team uses Microsoft Office, Adobe Creative Suite, Slack for communication, Zoom for meetings, and Salesforce for CRM.", | |
"Popular social media platforms like Instagram, TikTok, Twitter, LinkedIn, and YouTube are essential for digital marketing." | |
] | |
for ex in examples: | |
if st.button(f"Use example: {ex[:50]}..."): | |
st.session_state["text_input"] = ex | |
if __name__ == "__main__": | |
main() | |