nickyni commited on
Commit
4a7aa11
Β·
verified Β·
1 Parent(s): 7bcceab

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ NexaAPI Tutorial 2026 β€” Cheapest AI API Demo
3
+ Generate AI images with Flux, SDXL, and more for $0.003/image
4
+
5
+ Get your API key: https://rapidapi.com/user/nexaquency
6
+ Docs: https://nexa-api.com
7
+ """
8
+
9
+ import gradio as gr
10
+ import os
11
+ import requests
12
+
13
+ # NexaAPI configuration
14
+ NEXAAPI_BASE_URL = "https://api.nexa-api.com/v1"
15
+
16
+ MODELS = {
17
+ "Flux Schnell ($0.003/image) β€” Fastest": "flux-schnell",
18
+ "Flux Pro 1.1 ($0.02/image) β€” Best Quality": "flux-pro-1.1",
19
+ "Flux Dev ($0.01/image) β€” Balanced": "flux-dev",
20
+ "Stable Diffusion 3.5 ($0.005/image)": "stable-diffusion-3.5",
21
+ "Stable Diffusion XL ($0.003/image)": "stable-diffusion-xl",
22
+ }
23
+
24
+ PRICES = {
25
+ "flux-schnell": 0.003,
26
+ "flux-pro-1.1": 0.020,
27
+ "flux-dev": 0.010,
28
+ "stable-diffusion-3.5": 0.005,
29
+ "stable-diffusion-xl": 0.003,
30
+ }
31
+
32
+
33
+ def generate_image(api_key: str, prompt: str, model_display: str, width: int, height: int):
34
+ """Generate an image using NexaAPI."""
35
+ if not api_key:
36
+ return None, "❌ Please enter your NexaAPI key. Get one free at https://rapidapi.com/user/nexaquency"
37
+
38
+ if not prompt:
39
+ return None, "❌ Please enter a prompt"
40
+
41
+ model = MODELS.get(model_display, "flux-schnell")
42
+ price = PRICES.get(model, 0.003)
43
+
44
+ try:
45
+ # Use OpenAI-compatible endpoint
46
+ from openai import OpenAI
47
+ client = OpenAI(api_key=api_key, base_url=NEXAAPI_BASE_URL)
48
+
49
+ response = client.images.generate(
50
+ model=model,
51
+ prompt=prompt,
52
+ size=f"{width}x{height}",
53
+ n=1
54
+ )
55
+
56
+ image_url = response.data[0].url
57
+ cost_info = f"βœ… Generated! Cost: ${price:.3f} | Model: {model} | URL: {image_url}"
58
+ return image_url, cost_info
59
+
60
+ except Exception as e:
61
+ error_msg = str(e)
62
+ if "401" in error_msg or "unauthorized" in error_msg.lower():
63
+ return None, "❌ Invalid API key. Get your free key at https://rapidapi.com/user/nexaquency"
64
+ return None, f"❌ Error: {error_msg}"
65
+
66
+
67
+ # Gradio UI
68
+ with gr.Blocks(
69
+ title="NexaAPI β€” Cheapest AI Image Generation 2026",
70
+ theme=gr.themes.Soft()
71
+ ) as demo:
72
+ gr.Markdown("""
73
+ # πŸš€ NexaAPI β€” Cheapest AI Image Generation Demo
74
+
75
+ Access 50+ AI models for as little as **$0.003/image** β€” 5Γ— cheaper than official APIs.
76
+
77
+ **Get your free API key:** [https://rapidapi.com/user/nexaquency](https://rapidapi.com/user/nexaquency)
78
+ | **Docs:** [https://nexa-api.com](https://nexa-api.com)
79
+ | **GitHub:** [nexaapi-tutorial-2026](https://github.com/diwushennian4955/nexaapi-tutorial-2026)
80
+ """)
81
+
82
+ with gr.Row():
83
+ with gr.Column(scale=1):
84
+ api_key = gr.Textbox(
85
+ label="NexaAPI Key",
86
+ placeholder="Enter your API key from RapidAPI",
87
+ type="password"
88
+ )
89
+ prompt = gr.Textbox(
90
+ label="Prompt",
91
+ placeholder="A red panda coding at a computer, digital art style",
92
+ lines=3,
93
+ value="A red panda coding at a computer, digital art style"
94
+ )
95
+ model_select = gr.Dropdown(
96
+ label="Model",
97
+ choices=list(MODELS.keys()),
98
+ value="Flux Schnell ($0.003/image) β€” Fastest"
99
+ )
100
+ with gr.Row():
101
+ width = gr.Slider(512, 1024, value=1024, step=64, label="Width")
102
+ height = gr.Slider(512, 1024, value=1024, step=64, label="Height")
103
+ generate_btn = gr.Button("🎨 Generate Image", variant="primary")
104
+
105
+ with gr.Column(scale=1):
106
+ output_image = gr.Image(label="Generated Image", type="filepath")
107
+ cost_display = gr.Textbox(label="Status & Cost", interactive=False)
108
+
109
+ generate_btn.click(
110
+ fn=generate_image,
111
+ inputs=[api_key, prompt, model_select, width, height],
112
+ outputs=[output_image, cost_display]
113
+ )
114
+
115
+ gr.Markdown("""
116
+ ## πŸ’‘ Quick Start
117
+
118
+ ```python
119
+ # pip install nexaapi
120
+ from nexaapi import NexaAPI
121
+ client = NexaAPI(api_key="YOUR_API_KEY")
122
+ print(client.images.generate(model="flux-schnell", prompt="a red panda coding").image_url)
123
+ ```
124
+
125
+ ## πŸ“Š Pricing Comparison
126
+
127
+ | Model | NexaAPI | Official | Savings |
128
+ |-------|---------|----------|---------|
129
+ | Flux Schnell | **$0.003** | $0.015 | 5Γ— cheaper |
130
+ | Flux Pro 1.1 | **$0.02** | $0.055 | 2.75Γ— cheaper |
131
+ | Stable Diffusion 3.5 | **$0.005** | $0.02 | 4Γ— cheaper |
132
+
133
+ **Links:**
134
+ - 🌐 [nexa-api.com](https://nexa-api.com)
135
+ - πŸš€ [RapidAPI Hub](https://rapidapi.com/user/nexaquency)
136
+ - 🐍 [pip install nexaapi](https://pypi.org/project/nexaapi/)
137
+ - πŸ“¦ [npm install nexaapi](https://www.npmjs.com/package/nexaapi)
138
+ """)
139
+
140
+ if __name__ == "__main__":
141
+ demo.launch()