Spaces:
Running
Running
import gradio as gr | |
import pandas as pd | |
from main import SoundgasmScraper | |
import time | |
# Initialize the scraper | |
scraper = SoundgasmScraper() | |
def search_soundgasm(query, max_results=10): | |
""" | |
Search for audio on Soundgasm and return results in a formatted way | |
""" | |
if not query.strip(): | |
return "Please enter a search query.", None | |
try: | |
# Show loading message | |
results = scraper.search_audio(query, max_results=int(max_results)) | |
if not results: | |
return "No results found. Try a different search term.", None | |
# Format results for display | |
formatted_results = [] | |
for i, result in enumerate(results, 1): | |
formatted_result = f""" | |
### Result {i}: {result['audio_title'] or 'Untitled'} | |
**Username:** {result['username'] or 'Unknown'} | |
**Page URL:** [{result['url']}]({result['url']}) | |
**Direct Audio URL:** {result['audio_url'] or 'Not found'} | |
**Description:** {result['description'][:200] + '...' if result['description'] and len(result['description']) > 200 else result['description'] or 'No description available'} | |
--- | |
""" | |
formatted_results.append(formatted_result) | |
# Create a DataFrame for the table view | |
df_data = [] | |
for result in results: | |
df_data.append({ | |
'Title': result['audio_title'] or 'Untitled', | |
'Username': result['username'] or 'Unknown', | |
'Page URL': result['url'], | |
'Audio URL': result['audio_url'] or 'Not found', | |
'Description': (result['description'][:100] + '...' if result['description'] and len(result['description']) > 100 else result['description']) or 'No description' | |
}) | |
df = pd.DataFrame(df_data) | |
return "\n".join(formatted_results), df | |
except Exception as e: | |
return f"Error occurred during search: {str(e)}", None | |
def search_by_user(username): | |
""" | |
Search for all audios by a specific username | |
""" | |
if not username.strip(): | |
return "Please enter a username.", None | |
try: | |
results = scraper.search_by_username(username.strip()) | |
if not results: | |
return f"No audios found for user '{username}' or user doesn't exist.", None | |
# Format results for display | |
formatted_results = [] | |
for i, result in enumerate(results, 1): | |
formatted_result = f""" | |
### Audio {i}: {result['audio_title'] or 'Untitled'} | |
**Page URL:** [{result['url']}]({result['url']}) | |
**Direct Audio URL:** {result['audio_url'] or 'Not found'} | |
**Description:** {result['description'][:200] + '...' if result['description'] and len(result['description']) > 200 else result['description'] or 'No description available'} | |
--- | |
""" | |
formatted_results.append(formatted_result) | |
# Create a DataFrame for the table view | |
df_data = [] | |
for result in results: | |
df_data.append({ | |
'Title': result['audio_title'] or 'Untitled', | |
'Page URL': result['url'], | |
'Audio URL': result['audio_url'] or 'Not found', | |
'Description': (result['description'][:100] + '...' if result['description'] and len(result['description']) > 100 else result['description']) or 'No description' | |
}) | |
df = pd.DataFrame(df_data) | |
return "\n".join(formatted_results), df | |
except Exception as e: | |
return f"Error occurred during user search: {str(e)}", None | |
# Create the Gradio interface | |
with gr.Blocks(title="Soundgasm Audio Search", theme=gr.themes.Base()) as app: | |
gr.Markdown(""" | |
# π§ Soundgasm Audio Search | |
Search for audio content on Soundgasm.net and get direct links to the audio files. | |
**Features:** | |
- Search by keywords | |
- Search by username | |
- Get direct audio file URLs | |
- View audio descriptions and metadata | |
""") | |
with gr.Tabs(): | |
# Search by keywords tab | |
with gr.TabItem("π Search by Keywords"): | |
with gr.Row(): | |
with gr.Column(scale=3): | |
search_input = gr.Textbox( | |
label="Search Query", | |
placeholder="Enter keywords (e.g., ASMR, relaxing, etc.)", | |
lines=1 | |
) | |
with gr.Column(scale=1): | |
max_results_input = gr.Slider( | |
minimum=1, | |
maximum=20, | |
value=10, | |
step=1, | |
label="Max Results" | |
) | |
search_button = gr.Button("π Search", variant="primary") | |
with gr.Row(): | |
with gr.Column(): | |
search_output = gr.Markdown(label="Search Results") | |
with gr.Column(): | |
search_table = gr.Dataframe( | |
label="Results Table", | |
interactive=False, | |
wrap=True | |
) | |
# Search by username tab | |
with gr.TabItem("π€ Search by Username"): | |
username_input = gr.Textbox( | |
label="Username", | |
placeholder="Enter Soundgasm username", | |
lines=1 | |
) | |
user_search_button = gr.Button("π Search User", variant="primary") | |
with gr.Row(): | |
with gr.Column(): | |
user_output = gr.Markdown(label="User's Audios") | |
with gr.Column(): | |
user_table = gr.Dataframe( | |
label="User's Audios Table", | |
interactive=False, | |
wrap=True | |
) | |
# Instructions | |
with gr.Accordion("π How to Use", open=False): | |
gr.Markdown(""" | |
### Search by Keywords: | |
1. Enter keywords related to the audio you're looking for | |
2. Adjust the maximum number of results if needed | |
3. Click "Search" to find matching audios | |
### Search by Username: | |
1. Enter a Soundgasm username | |
2. Click "Search User" to see all audios from that user | |
### Results: | |
- **Page URL**: Link to the Soundgasm page | |
- **Audio URL**: Direct link to the audio file (m4a format) | |
- **Description**: Audio description if available | |
### Note: | |
- This tool searches using external search engines, so results may vary | |
- Some audio URLs might not be extractable due to page structure changes | |
- Always respect the content creators' rights and terms of service | |
""") | |
# Event handlers | |
search_button.click( | |
fn=search_soundgasm, | |
inputs=[search_input, max_results_input], | |
outputs=[search_output, search_table] | |
) | |
user_search_button.click( | |
fn=search_by_user, | |
inputs=[username_input], | |
outputs=[user_output, user_table] | |
) | |
# Allow Enter key to trigger search | |
search_input.submit( | |
fn=search_soundgasm, | |
inputs=[search_input, max_results_input], | |
outputs=[search_output, search_table] | |
) | |
username_input.submit( | |
fn=search_by_user, | |
inputs=[username_input], | |
outputs=[user_output, user_table] | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
app.launch( | |
server_name="0.0.0.0", | |
server_port=7860, | |
share=False, | |
debug=True | |
) | |