Spaces:
Running
Running
File size: 14,707 Bytes
4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 d0393bd 4a7aa11 | 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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | """
AI API Playground 2026 β NexaAPI Interactive Demo
Tabs: Image Generation | Text/LLM | Text-to-Speech | Cost Calculator
"""
import gradio as gr
# βββ Cost Calculator Data βββββββββββββββββββββββββββββββββββββββββββββββββββ
PRICING = {
"Image Generation (per image)": {
"NexaAPI π": 0.003,
"OpenAI DALL-E 3": 0.040,
"Stability AI": 0.020,
"Replicate": 0.015,
"FAL.ai": 0.012,
},
"LLM (per 1M tokens)": {
"NexaAPI π": 0.50,
"OpenAI GPT-4o": 5.00,
"Anthropic Claude 3.5": 3.00,
"Google Gemini 1.5 Pro": 3.50,
"Together AI": 1.00,
},
"Text-to-Speech (per 1M chars)": {
"NexaAPI π": 1.00,
"ElevenLabs": 22.00,
"OpenAI TTS": 15.00,
"Google TTS": 4.00,
"Azure TTS": 4.00,
},
}
# βββ Tab Functions βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def show_image_demo(prompt, style):
"""Show image generation demo with code."""
styles = {
"Photorealistic": "photorealistic, 8k, detailed",
"Anime": "anime style, vibrant colors",
"Oil Painting": "oil painting, classical art style",
"Watercolor": "watercolor painting, soft colors",
}
style_suffix = styles.get(style, "")
full_prompt = f"{prompt}, {style_suffix}" if style_suffix else prompt
code = f'''```python
import nexaapi
# Image Generation β $0.003/image (13x cheaper than DALL-E 3!)
client = nexaapi.Client(api_key="YOUR_API_KEY")
result = client.image.generate(
prompt="{full_prompt}",
model="flux-schnell", # or stable-diffusion-xl, flux-pro
width=1024,
height=1024,
steps=4
)
# Save the image
with open("output.png", "wb") as f:
f.write(result.image_bytes)
print(f"Generated! Cost: $0.003")
print(f"URL: {{result.url}}")
```
**vs OpenAI DALL-E 3**: Same quality, 13x cheaper ($0.003 vs $0.04)
### π Cost for 1,000 images:
- **NexaAPI**: $3.00 β
- OpenAI DALL-E 3: $40.00 β
- Stability AI: $20.00 β
- FAL.ai: $12.00 β
### π Get Started Free
- [nexa-api.com](https://nexa-api.com) β 100 free images, no credit card!
- [pip install nexaapi](https://pypi.org/project/nexaapi)
'''
preview = f"""
### πΌοΈ Your Prompt
**"{full_prompt}"**
This would generate a high-quality image using NexaAPI's FLUX or Stable Diffusion models.
**Cost**: $0.003 per image
**Speed**: ~2-4 seconds
**Resolution**: Up to 2048Γ2048
*Note: This is a demo playground. Sign up at [nexa-api.com](https://nexa-api.com) to generate real images!*
"""
return preview, code
def show_llm_demo(prompt, model):
"""Show LLM demo with code."""
models_info = {
"Claude 3.5 Sonnet": ("claude-3-5-sonnet", "$0.50/1M tokens"),
"GPT-4o": ("gpt-4o", "$0.50/1M tokens"),
"Gemini 1.5 Pro": ("gemini-1.5-pro", "$0.50/1M tokens"),
"Llama 3.1 70B": ("llama-3.1-70b", "$0.30/1M tokens"),
}
model_id, price = models_info.get(model, ("claude-3-5-sonnet", "$0.50/1M tokens"))
sample_responses = {
"Claude 3.5 Sonnet": f"I'd be happy to help with: '{prompt}'. This is a demonstration of NexaAPI's LLM capabilities β access Claude, GPT-4o, Gemini, and 50+ more models through one unified API at the lowest prices in 2026.",
"GPT-4o": f"Regarding your query: '{prompt}' β NexaAPI provides access to GPT-4o and all major AI models at 90% less cost than going directly to OpenAI.",
"Gemini 1.5 Pro": f"Processing your request: '{prompt}' β With NexaAPI, you get Gemini 1.5 Pro access at $0.50/1M tokens vs Google's $3.50/1M tokens.",
"Llama 3.1 70B": f"Here's my response to '{prompt}' β Llama 3.1 70B via NexaAPI is one of the most cost-effective options for high-quality open-source LLM inference.",
}
response = sample_responses.get(model, f"Response to: {prompt}")
code = f'''```python
import nexaapi
# LLM β 90% cheaper than direct API access!
client = nexaapi.Client(api_key="YOUR_API_KEY")
response = client.chat.complete(
model="{model_id}",
messages=[
{{"role": "user", "content": "{prompt}"}}
],
max_tokens=1000
)
print(response.choices[0].message.content)
print(f"Tokens used: {{response.usage.total_tokens}}")
print(f"Cost: {{response.usage.total_tokens / 1_000_000 * 0.50:.6f}}")
```
**Price**: {price} (vs $5.00/1M tokens for OpenAI direct)
### π Get Started Free
- [nexa-api.com](https://nexa-api.com) β Free tier available!
- [pip install nexaapi](https://pypi.org/project/nexaapi)
'''
return f"**{model} Response (Demo):**\n\n{response}", code
def show_tts_demo(text, voice):
"""Show TTS demo with code."""
voices = {
"Nova (Female, Warm)": "nova",
"Alloy (Neutral)": "alloy",
"Echo (Male, Deep)": "echo",
"Shimmer (Female, Clear)": "shimmer",
}
voice_id = voices.get(voice, "nova")
code = f'''```python
import nexaapi
# Text-to-Speech β 22x cheaper than ElevenLabs!
client = nexaapi.Client(api_key="YOUR_API_KEY")
audio = client.tts.generate(
text="{text[:50]}...",
voice="{voice_id}",
model="tts-1-hd",
speed=1.0
)
# Save audio file
with open("output.mp3", "wb") as f:
f.write(audio.audio_bytes)
print(f"Generated {len(text)} characters")
print(f"Cost: ${{len(text) / 1_000_000:.6f}}") # $1.00/1M chars
```
**Price**: $1.00/1M characters
- vs ElevenLabs: $22.00/1M chars (22x more expensive!)
- vs OpenAI TTS: $15.00/1M chars (15x more expensive!)
### π Get Started Free
- [nexa-api.com](https://nexa-api.com) β 100 free TTS generations!
- [pip install nexaapi](https://pypi.org/project/nexaapi)
'''
char_count = len(text)
cost = char_count / 1_000_000 * 1.00
elevenlabs_cost = char_count / 1_000_000 * 22.00
preview = f"""
### π TTS Preview
**Text**: "{text[:100]}{'...' if len(text) > 100 else ''}"
**Voice**: {voice}
**Characters**: {char_count:,}
**Cost with NexaAPI**: ${cost:.6f}
**Cost with ElevenLabs**: ${elevenlabs_cost:.6f}
**Your savings**: ${elevenlabs_cost - cost:.6f} (22x cheaper!)
*Note: This is a demo. Sign up at [nexa-api.com](https://nexa-api.com) to generate real audio!*
"""
return preview, code
def calculate_costs(api_type, quantity):
"""Calculate and compare costs."""
if quantity <= 0:
return "Please enter a quantity > 0"
prices = PRICING.get(api_type, {})
if not prices:
return "Invalid API type"
nexa_cost = prices["NexaAPI π"] * quantity
lines = [f"## Cost for {quantity:,} units β {api_type}\n"]
lines.append("| Provider | Total Cost | vs NexaAPI |")
lines.append("|----------|-----------|------------|")
for provider, price in sorted(prices.items(), key=lambda x: x[1]):
total = price * quantity
if provider == "NexaAPI π":
lines.append(f"| **{provider}** | **${total:.2f}** | β
Best Price! |")
else:
mult = total / nexa_cost
lines.append(f"| {provider} | ${total:.2f} | {mult:.1f}x more expensive |")
lines.append(f"\nπ‘ **NexaAPI saves you ${(max(prices.values()) - prices['NexaAPI π']) * quantity:.2f}** vs the most expensive provider!")
lines.append(f"\nπ [Start Free at nexa-api.com](https://nexa-api.com) β No credit card required!")
return "\n".join(lines)
# βββ Build UI ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.Blocks(
title="AI API Playground 2026 β NexaAPI",
theme=gr.themes.Soft(),
) as demo:
gr.Markdown("""
# π AI API Playground 2026
## Access 56+ AI Models via One API β NexaAPI
**The cheapest way to access Image Generation, LLM, TTS, and Video APIs in 2026.**
Start with **100 free generations** β no credit card required!
π [nexa-api.com](https://nexa-api.com) | π¦ [PyPI](https://pypi.org/project/nexaapi) | π¦ [npm](https://npmjs.com/package/nexaapi) | π [RapidAPI](https://rapidapi.com/user/nexaquency)
""")
with gr.Tabs():
# ββ Tab 1: Image Generation ββββββββββββββββββββββββββββββββββββββββββ
with gr.TabItem("πΌοΈ Image Generation"):
gr.Markdown("### Generate AI Images β $0.003/image (13x cheaper than DALL-E 3!)")
with gr.Row():
with gr.Column():
img_prompt = gr.Textbox(
label="Image Prompt",
placeholder="A futuristic city at sunset with flying cars...",
value="A beautiful mountain landscape with aurora borealis",
lines=3
)
img_style = gr.Dropdown(
choices=["Photorealistic", "Anime", "Oil Painting", "Watercolor"],
value="Photorealistic",
label="Style"
)
img_btn = gr.Button("π¨ Generate Code & Preview", variant="primary")
with gr.Column():
img_preview = gr.Markdown(label="Preview")
img_code = gr.Markdown(label="Code Example")
img_btn.click(show_image_demo, inputs=[img_prompt, img_style], outputs=[img_preview, img_code])
# ββ Tab 2: LLM / Text ββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.TabItem("π¬ LLM / Text"):
gr.Markdown("### Access 20+ LLM Models β From $0.30/1M tokens (10x cheaper than GPT-4o!)")
with gr.Row():
with gr.Column():
llm_prompt = gr.Textbox(
label="Your Prompt",
placeholder="Explain quantum computing in simple terms...",
value="What are the key advantages of using NexaAPI for AI development?",
lines=4
)
llm_model = gr.Dropdown(
choices=["Claude 3.5 Sonnet", "GPT-4o", "Gemini 1.5 Pro", "Llama 3.1 70B"],
value="Claude 3.5 Sonnet",
label="Model"
)
llm_btn = gr.Button("π€ Generate Response & Code", variant="primary")
with gr.Column():
llm_response = gr.Markdown(label="Response Preview")
llm_code = gr.Markdown(label="Code Example")
llm_btn.click(show_llm_demo, inputs=[llm_prompt, llm_model], outputs=[llm_response, llm_code])
# ββ Tab 3: TTS βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with gr.TabItem("π Text-to-Speech"):
gr.Markdown("### Convert Text to Speech β $1.00/1M chars (22x cheaper than ElevenLabs!)")
with gr.Row():
with gr.Column():
tts_text = gr.Textbox(
label="Text to Convert",
placeholder="Enter text to convert to speech...",
value="Welcome to NexaAPI β the most affordable AI API platform in 2026. Access 56 models including Claude, GPT-4o, Gemini, FLUX, and more.",
lines=4
)
tts_voice = gr.Dropdown(
choices=["Nova (Female, Warm)", "Alloy (Neutral)", "Echo (Male, Deep)", "Shimmer (Female, Clear)"],
value="Nova (Female, Warm)",
label="Voice"
)
tts_btn = gr.Button("ποΈ Generate Code & Preview", variant="primary")
with gr.Column():
tts_preview = gr.Markdown(label="Preview")
tts_code = gr.Markdown(label="Code Example")
tts_btn.click(show_tts_demo, inputs=[tts_text, tts_voice], outputs=[tts_preview, tts_code])
# ββ Tab 4: Cost Calculator βββββββββββββββββββββββββββββββββββββββββββ
with gr.TabItem("π° Cost Calculator"):
gr.Markdown("### Calculate Your Savings β See How Much You Overpay with Other Providers")
with gr.Row():
with gr.Column(scale=1):
calc_api = gr.Dropdown(
choices=list(PRICING.keys()),
value="Image Generation (per image)",
label="API Type"
)
calc_qty = gr.Number(
value=10000,
label="Monthly Usage (units)",
minimum=1
)
calc_btn = gr.Button("π° Calculate Costs", variant="primary")
with gr.Column(scale=2):
calc_results = gr.Markdown(label="Cost Comparison")
calc_btn.click(calculate_costs, inputs=[calc_api, calc_qty], outputs=[calc_results])
gr.Markdown("""
---
## π Why NexaAPI?
- **56+ Models**: Claude, GPT-4o, Gemini, FLUX, Stable Diffusion, Kling, Sora alternatives & more
- **Lowest Prices**: Up to 95% cheaper than going directly to model providers
- **One API**: OpenAI-compatible β works with your existing code
- **Free Tier**: 100 free generations, no credit card required
- **Fast**: Global CDN, <100ms latency
### π Get Started Now
| Platform | Link |
|----------|------|
| π Website | [nexa-api.com](https://nexa-api.com) |
| π Python | [pip install nexaapi](https://pypi.org/project/nexaapi) |
| π¦ JavaScript | [npm install nexaapi](https://npmjs.com/package/nexaapi) |
| π RapidAPI | [rapidapi.com/user/nexaquency](https://rapidapi.com/user/nexaquency) |
""")
if __name__ == "__main__":
demo.launch()
|