Alireza1913 commited on
Commit
a07cb5f
Β·
verified Β·
1 Parent(s): edf7907

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +235 -0
app.py ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from huggingface_hub import InferenceClient
4
+
5
+ HF_TOKEN = os.environ.get("HF_TOKEN", "")
6
+ MODEL = "mistralai/Mistral-7B-Instruct-v0.3"
7
+
8
+ SYSTEM_PROMPT = """You are an expert API architect and indie hacker advisor.
9
+
10
+ You know every major API deeply:
11
+
12
+ PAYMENT: Stripe, Paddle, LemonSqueezy, PayPal, Razorpay
13
+ AI/ML: OpenAI, Anthropic Claude, Replicate, HuggingFace Inference, AssemblyAI, ElevenLabs
14
+ COMMUNICATION: Twilio (SMS/Voice), SendGrid (email), Resend, Mailgun, WhatsApp Business
15
+ DATA/ENRICHMENT: Clearbit, Apollo.io, Hunter.io, Proxycurl
16
+ MAPS/LOCATION: Google Maps, Mapbox, HERE, OpenStreetMap
17
+ MEDIA: Cloudinary, Mux (video), Imgix, Transloadit
18
+ FINANCE: Alpha Vantage, Polygon.io, CoinGecko, Plaid, Finnhub
19
+ SEARCH: Algolia, Typesense, Meilisearch
20
+ AUTH: Auth0, Clerk, Supabase Auth, Firebase Auth
21
+ DATABASE: Supabase, Firebase, PlanetScale, Neon, Upstash
22
+ SCRAPING: Apify, ScraperAPI, Browserless, Firecrawl
23
+ ANALYTICS: Mixpanel, PostHog, Amplitude, Plausible
24
+
25
+ When a user describes their product idea, recommend the perfect API stack.
26
+
27
+ Your response MUST follow this exact format:
28
+
29
+ ## 🎯 Your Idea in One Line
30
+ (restate the idea clearly)
31
+
32
+ ## πŸ”§ Recommended API Stack
33
+
34
+ For each API (recommend 3-5 total):
35
+
36
+ ### [API Name] β€” [Role in the product]
37
+ - **What it does for you:** (one sentence)
38
+ - **Pricing:** (free tier + paid, be specific with numbers)
39
+ - **Docs:** (exact URL)
40
+ - **Why not alternatives:** (one sentence)
41
+ - **Integration difficulty:** Easy / Medium / Hard
42
+
43
+ ## πŸ’° Monthly Cost Estimate
44
+ | Users | Est. Cost |
45
+ |-------|-----------|
46
+ | 100 | $X/month |
47
+ | 1,000 | $X/month |
48
+ | 10,000| $X/month |
49
+
50
+ ## ⚑ Build Order
51
+ 1. First: [API] β€” because...
52
+ 2. Second: [API] β€” because...
53
+ 3. Third: [API] β€” because...
54
+
55
+ ## 🚨 Top Mistake to Avoid
56
+ (the most common mistake with this stack)
57
+
58
+ Be specific, honest about pricing, always mention free tiers."""
59
+
60
+
61
+ def get_stack(idea, budget, level, market):
62
+ if not idea.strip():
63
+ return "⚠️ Please describe your product idea."
64
+
65
+ if not HF_TOKEN:
66
+ return (
67
+ "⚠️ **HF_TOKEN is not set.**\n\n"
68
+ "Go to: **Space Settings β†’ Secrets β†’ New secret**\n"
69
+ "- Name: `HF_TOKEN`\n"
70
+ "- Value: your token from [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)\n\n"
71
+ "Free token β€” no credit card needed!"
72
+ )
73
+
74
+ try:
75
+ client = InferenceClient(token=HF_TOKEN)
76
+
77
+ prompt = f"""Product idea: {idea}
78
+
79
+ Monthly API budget: {budget}
80
+ Technical level: {level}
81
+ Target market: {market}
82
+
83
+ Recommend the best API stack for this product."""
84
+
85
+ messages = [
86
+ {"role": "system", "content": SYSTEM_PROMPT},
87
+ {"role": "user", "content": prompt}
88
+ ]
89
+
90
+ response = client.chat.completions.create(
91
+ model=MODEL,
92
+ messages=messages,
93
+ max_tokens=1500,
94
+ temperature=0.7
95
+ )
96
+ return response.choices[0].message.content
97
+
98
+ except Exception as e:
99
+ err = str(e)
100
+ if "401" in err or "auth" in err.lower():
101
+ return "❌ Invalid token. Check your HF_TOKEN in Space Settings."
102
+ if "429" in err or "rate" in err.lower():
103
+ return "❌ Rate limit hit. Wait 30 seconds and try again."
104
+ if "loading" in err.lower():
105
+ return "⏳ Model is loading (cold start). Wait 20 seconds and try again."
106
+ return f"❌ Error: {err}"
107
+
108
+
109
+ def ask_followup(question, prev):
110
+ if not question.strip():
111
+ return ""
112
+ if not prev or prev.startswith("⚠️") or prev.startswith("❌"):
113
+ return "⚠️ Please generate a stack recommendation first."
114
+
115
+ try:
116
+ client = InferenceClient(token=HF_TOKEN)
117
+ messages = [
118
+ {"role": "system", "content": SYSTEM_PROMPT},
119
+ {
120
+ "role": "user",
121
+ "content": (
122
+ f"Previous recommendation:\n{prev}\n\n"
123
+ f"Follow-up question: {question}\n\n"
124
+ f"Answer concisely and helpfully."
125
+ )
126
+ }
127
+ ]
128
+ response = client.chat.completions.create(
129
+ model=MODEL,
130
+ messages=messages,
131
+ max_tokens=800,
132
+ temperature=0.7
133
+ )
134
+ return response.choices[0].message.content
135
+
136
+ except Exception as e:
137
+ return f"❌ Error: {str(e)}"
138
+
139
+
140
+ EXAMPLES = [
141
+ ["A Notion-like notes app with AI summarization", "$50/month", "Intermediate", "Students"],
142
+ ["SMS marketing platform for small restaurants", "$30/month", "Beginner", "Restaurant owners"],
143
+ ["Job board that auto-matches candidates with AI", "$100/month", "Advanced", "Tech recruiters"],
144
+ ["Podcast transcription and highlight tool", "$20/month", "Beginner", "Podcasters"],
145
+ ["Crypto portfolio tracker with price alerts", "$0 (free only)", "Intermediate", "Retail investors"],
146
+ ]
147
+
148
+ css = """
149
+ .gradio-container { max-width: 900px !important; margin: auto; }
150
+ footer { display: none !important; }
151
+ """
152
+
153
+ with gr.Blocks(css=css, title="API Stack Finder") as demo:
154
+ gr.Markdown("""
155
+ # πŸ”§ API Stack Finder
156
+ **Describe your product idea β†’ Get the perfect API stack, pricing, and build order**
157
+
158
+ Powered by **Mistral-7B-Instruct** via HuggingFace Inference API β€” 100% Free.
159
+ """)
160
+
161
+ with gr.Row():
162
+ with gr.Column(scale=3):
163
+ idea_input = gr.Textbox(
164
+ label="Describe your product idea",
165
+ placeholder="e.g. A platform where freelancers can sell services and get paid instantly...",
166
+ lines=4
167
+ )
168
+ with gr.Column(scale=1):
169
+ budget_input = gr.Dropdown(
170
+ choices=[
171
+ "$0 (free only)",
172
+ "$10/month",
173
+ "$30/month",
174
+ "$50/month",
175
+ "$100/month",
176
+ "$500/month",
177
+ "Unlimited"
178
+ ],
179
+ value="$30/month",
180
+ label="Monthly API budget"
181
+ )
182
+ level_input = gr.Dropdown(
183
+ choices=["Beginner", "Intermediate", "Advanced"],
184
+ value="Intermediate",
185
+ label="Technical level"
186
+ )
187
+ market_input = gr.Textbox(
188
+ label="Target market",
189
+ placeholder="e.g. Small business owners",
190
+ value="General consumers"
191
+ )
192
+
193
+ gr.Markdown("**Try an example:**")
194
+ with gr.Row():
195
+ for ex in EXAMPLES:
196
+ gr.Button(ex[0][:32] + "...", size="sm").click(
197
+ lambda e=ex: (e[0], e[1], e[2], e[3]),
198
+ outputs=[idea_input, budget_input, level_input, market_input]
199
+ )
200
+
201
+ submit_btn = gr.Button("πŸ” Find My API Stack", variant="primary", size="lg")
202
+ output = gr.Markdown("*Your API stack will appear here...*")
203
+
204
+ gr.Markdown("---")
205
+ gr.Markdown("### πŸ’¬ Ask a follow-up")
206
+
207
+ with gr.Row():
208
+ followup_input = gr.Textbox(
209
+ label="Follow-up question",
210
+ placeholder="e.g. Is there a free alternative to Stripe? / How do I add auth cheaply?",
211
+ scale=4
212
+ )
213
+ followup_btn = gr.Button("Ask", scale=1, variant="secondary")
214
+
215
+ followup_output = gr.Markdown()
216
+
217
+ submit_btn.click(
218
+ get_stack,
219
+ inputs=[idea_input, budget_input, level_input, market_input],
220
+ outputs=output
221
+ )
222
+ followup_btn.click(
223
+ ask_followup,
224
+ inputs=[followup_input, output],
225
+ outputs=followup_output
226
+ )
227
+
228
+ gr.Markdown("""
229
+ ---
230
+ πŸ€– Model: `Mistral-7B-Instruct-v0.3` Β· πŸ†“ Free via HuggingFace Inference API
231
+ πŸ”‘ Get your free token: [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
232
+ """)
233
+
234
+ if __name__ == "__main__":
235
+ demo.launch()