Alireza1913 commited on
Commit
e8c7792
Β·
verified Β·
1 Parent(s): 6939b23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -90
app.py CHANGED
@@ -1,9 +1,9 @@
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
 
@@ -20,42 +20,38 @@ 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):
@@ -71,39 +67,42 @@ def get_stack(idea, budget, level, market):
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):
@@ -112,29 +111,17 @@ def ask_followup(question, prev):
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 = [
@@ -155,7 +142,7 @@ with gr.Blocks(css=css, title="API Stack Finder") as demo:
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():
@@ -168,13 +155,8 @@ Powered by **Mistral-7B-Instruct** via HuggingFace Inference API β€” 100% Free.
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"
@@ -207,7 +189,7 @@ Powered by **Mistral-7B-Instruct** via HuggingFace Inference API β€” 100% Free.
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")
@@ -227,8 +209,8 @@ Powered by **Mistral-7B-Instruct** via HuggingFace Inference API β€” 100% Free.
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__":
 
1
  import gradio as gr
2
  import os
3
+ import requests
4
 
5
  HF_TOKEN = os.environ.get("HF_TOKEN", "")
6
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"
7
 
8
  SYSTEM_PROMPT = """You are an expert API architect and indie hacker advisor.
9
 
 
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
 
26
+ def query_model(prompt):
27
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
28
 
29
+ full_prompt = f"<s>[INST] {SYSTEM_PROMPT}\n\n{prompt} [/INST]"
 
30
 
31
+ payload = {
32
+ "inputs": full_prompt,
33
+ "parameters": {
34
+ "max_new_tokens": 1200,
35
+ "temperature": 0.7,
36
+ "return_full_text": False
37
+ }
38
+ }
39
 
40
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
41
 
42
+ if response.status_code == 503:
43
+ return None, "⏳ Model is loading. Please wait 20 seconds and try again."
44
+ if response.status_code == 401:
45
+ return None, "❌ Invalid HF_TOKEN. Check Space Settings β†’ Secrets."
46
+ if response.status_code == 429:
47
+ return None, "❌ Rate limit hit. Wait 30 seconds and try again."
48
+ if response.status_code != 200:
49
+ return None, f"❌ Error {response.status_code}: {response.text[:200]}"
50
 
51
+ result = response.json()
52
+ if isinstance(result, list) and len(result) > 0:
53
+ return result[0].get("generated_text", ""), None
54
+ return None, f"❌ Unexpected response: {str(result)[:200]}"
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
 
57
  def get_stack(idea, budget, level, market):
 
67
  "Free token β€” no credit card needed!"
68
  )
69
 
70
+ prompt = f"""Product idea: {idea}
 
 
 
 
71
  Monthly API budget: {budget}
72
  Technical level: {level}
73
  Target market: {market}
74
 
75
+ Recommend the best API stack. Use this format:
76
+
77
+ ## 🎯 Your Idea in One Line
78
+ (restate clearly)
79
 
80
+ ## πŸ”§ Recommended API Stack
81
+ For each API (3-5 total):
82
+ ### [API Name] β€” [Role]
83
+ - What it does for you: (one sentence)
84
+ - Pricing: (free tier + paid with numbers)
85
+ - Docs: (exact URL)
86
+ - Integration difficulty: Easy / Medium / Hard
87
 
88
+ ## πŸ’° Monthly Cost Estimate
89
+ | Users | Est. Cost |
90
+ |-------|-----------|
91
+ | 100 | $X/month |
92
+ | 1,000 | $X/month |
93
+ | 10,000 | $X/month |
 
94
 
95
+ ## ⚑ Build Order
96
+ 1. First: [API] β€” because...
97
+ 2. Second: [API] β€” because...
98
+
99
+ ## 🚨 Top Mistake to Avoid
100
+ (most common mistake)"""
101
+
102
+ result, error = query_model(prompt)
103
+ if error:
104
+ return error
105
+ return result
106
 
107
 
108
  def ask_followup(question, prev):
 
111
  if not prev or prev.startswith("⚠️") or prev.startswith("❌"):
112
  return "⚠️ Please generate a stack recommendation first."
113
 
114
+ prompt = f"""Previous recommendation:
115
+ {prev[:500]}
116
+
117
+ Follow-up question: {question}
118
+
119
+ Answer concisely and helpfully."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
+ result, error = query_model(prompt)
122
+ if error:
123
+ return error
124
+ return result
125
 
126
 
127
  EXAMPLES = [
 
142
  # πŸ”§ API Stack Finder
143
  **Describe your product idea β†’ Get the perfect API stack, pricing, and build order**
144
 
145
+ Powered by **Mistral-7B** via HuggingFace Inference API β€” 100% Free.
146
  """)
147
 
148
  with gr.Row():
 
155
  with gr.Column(scale=1):
156
  budget_input = gr.Dropdown(
157
  choices=[
158
+ "$0 (free only)", "$10/month", "$30/month",
159
+ "$50/month", "$100/month", "$500/month", "Unlimited"
 
 
 
 
 
160
  ],
161
  value="$30/month",
162
  label="Monthly API budget"
 
189
  with gr.Row():
190
  followup_input = gr.Textbox(
191
  label="Follow-up question",
192
+ placeholder="e.g. Is there a free alternative to Stripe?",
193
  scale=4
194
  )
195
  followup_btn = gr.Button("Ask", scale=1, variant="secondary")
 
209
 
210
  gr.Markdown("""
211
  ---
212
+ πŸ€– Model: `Mistral-7B-Instruct-v0.3` Β· πŸ†“ Free via HuggingFace
213
+ πŸ”‘ Token: [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
214
  """)
215
 
216
  if __name__ == "__main__":