Spaces:
Sleeping
Sleeping
File size: 7,673 Bytes
d316107 d9925ae d316107 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
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
)
|