Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,134 +1,93 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
-
import
|
| 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 |
-
return None, "β³ Model is loading (cold start). Wait 30 seconds and try again."
|
| 39 |
-
if response.status_code == 401:
|
| 40 |
-
return None, "β Invalid HF_TOKEN. Go to Space Settings and check your token."
|
| 41 |
-
if response.status_code == 429:
|
| 42 |
-
return None, "β Rate limit. Wait 1 minute and try again."
|
| 43 |
-
if response.status_code != 200:
|
| 44 |
-
return None, f"β API Error {response.status_code}: {response.text[:300]}"
|
| 45 |
-
|
| 46 |
-
data = response.json()
|
| 47 |
-
|
| 48 |
-
if isinstance(data, list) and len(data) > 0:
|
| 49 |
-
text = data[0].get("generated_text", "").strip()
|
| 50 |
-
if text:
|
| 51 |
-
return text, None
|
| 52 |
-
|
| 53 |
-
if isinstance(data, dict) and "error" in data:
|
| 54 |
-
return None, f"β Model error: {data['error']}"
|
| 55 |
-
|
| 56 |
-
return None, f"β Empty response from model. Try again."
|
| 57 |
-
|
| 58 |
-
except requests.exceptions.Timeout:
|
| 59 |
-
return None, "β³ Request timed out. The model may be loading. Try again in 30 seconds."
|
| 60 |
-
except Exception as e:
|
| 61 |
-
return None, f"β Error: {str(e)}"
|
| 62 |
|
| 63 |
|
| 64 |
def get_stack(idea, budget, level, market):
|
| 65 |
if not idea.strip():
|
| 66 |
return "β οΈ Please describe your product idea."
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
My monthly API budget: {budget}
|
| 71 |
-
My technical level: {level}
|
| 72 |
-
My target market: {market}
|
| 73 |
-
|
| 74 |
-
Please recommend the best API stack for my product using this format:
|
| 75 |
-
|
| 76 |
-
## π― My Idea
|
| 77 |
-
(one sentence summary)
|
| 78 |
|
| 79 |
-
##
|
|
|
|
| 80 |
|
| 81 |
-
##
|
| 82 |
-
|
| 83 |
-
- Pricing: (free tier
|
| 84 |
- Docs: (URL)
|
| 85 |
-
- Difficulty: Easy
|
| 86 |
-
|
| 87 |
-
(repeat for 3-5 APIs)
|
| 88 |
|
| 89 |
-
## π°
|
| 90 |
- 100 users: $X/month
|
| 91 |
- 1,000 users: $X/month
|
| 92 |
-
- 10,000 users: $X/month
|
| 93 |
|
| 94 |
## β‘ Build Order
|
| 95 |
-
1.
|
| 96 |
-
2.
|
| 97 |
-
3. Finally [API] because...
|
| 98 |
|
| 99 |
-
## π¨
|
| 100 |
(one paragraph)"""
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
return error
|
| 105 |
-
return result if result else "β No response received. Please try again."
|
| 106 |
|
| 107 |
|
| 108 |
def ask_followup(question, prev):
|
| 109 |
if not question.strip():
|
| 110 |
return ""
|
| 111 |
if not prev or prev.startswith("β οΈ") or prev.startswith("β"):
|
| 112 |
-
return "β οΈ Generate a
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
{question}"""
|
| 119 |
-
|
| 120 |
-
result, error = query_model(prompt)
|
| 121 |
-
if error:
|
| 122 |
-
return error
|
| 123 |
-
return result if result else "β No response. Please try again."
|
| 124 |
|
| 125 |
|
| 126 |
EXAMPLES = [
|
| 127 |
-
["A
|
| 128 |
-
["SMS marketing
|
| 129 |
-
["
|
| 130 |
-
["Podcast transcription
|
| 131 |
-
["Crypto portfolio tracker
|
| 132 |
]
|
| 133 |
|
| 134 |
css = """
|
|
@@ -139,24 +98,22 @@ footer { display: none !important; }
|
|
| 139 |
with gr.Blocks(css=css, title="API Stack Finder") as demo:
|
| 140 |
gr.Markdown("""
|
| 141 |
# π§ API Stack Finder
|
| 142 |
-
**Describe your product idea β Get the perfect API stack
|
| 143 |
|
| 144 |
-
Powered by **
|
| 145 |
""")
|
| 146 |
|
| 147 |
with gr.Row():
|
| 148 |
with gr.Column(scale=3):
|
| 149 |
idea_input = gr.Textbox(
|
| 150 |
label="Describe your product idea",
|
| 151 |
-
placeholder="e.g. A
|
| 152 |
lines=4
|
| 153 |
)
|
| 154 |
with gr.Column(scale=1):
|
| 155 |
budget_input = gr.Dropdown(
|
| 156 |
-
choices=[
|
| 157 |
-
|
| 158 |
-
"$50/month", "$100/month", "$500/month", "Unlimited"
|
| 159 |
-
],
|
| 160 |
value="$30/month",
|
| 161 |
label="Monthly API budget"
|
| 162 |
)
|
|
@@ -174,7 +131,7 @@ Powered by **Zephyr-7B** via HuggingFace Inference API β 100% Free.
|
|
| 174 |
gr.Markdown("**Try an example:**")
|
| 175 |
with gr.Row():
|
| 176 |
for ex in EXAMPLES:
|
| 177 |
-
gr.Button(ex[0][:
|
| 178 |
lambda e=ex: (e[0], e[1], e[2], e[3]),
|
| 179 |
outputs=[idea_input, budget_input, level_input, market_input]
|
| 180 |
)
|
|
@@ -184,7 +141,6 @@ Powered by **Zephyr-7B** via HuggingFace Inference API β 100% Free.
|
|
| 184 |
|
| 185 |
gr.Markdown("---")
|
| 186 |
gr.Markdown("### π¬ Ask a follow-up")
|
| 187 |
-
|
| 188 |
with gr.Row():
|
| 189 |
followup_input = gr.Textbox(
|
| 190 |
label="Follow-up question",
|
|
@@ -192,25 +148,16 @@ Powered by **Zephyr-7B** via HuggingFace Inference API β 100% Free.
|
|
| 192 |
scale=4
|
| 193 |
)
|
| 194 |
followup_btn = gr.Button("Ask", scale=1, variant="secondary")
|
| 195 |
-
|
| 196 |
followup_output = gr.Markdown()
|
| 197 |
|
| 198 |
-
submit_btn.click(
|
| 199 |
-
get_stack,
|
| 200 |
inputs=[idea_input, budget_input, level_input, market_input],
|
| 201 |
-
outputs=output
|
| 202 |
-
|
| 203 |
-
followup_btn.click(
|
| 204 |
-
ask_followup,
|
| 205 |
inputs=[followup_input, output],
|
| 206 |
-
outputs=followup_output
|
| 207 |
-
)
|
| 208 |
|
| 209 |
-
gr.Markdown(""
|
| 210 |
-
---
|
| 211 |
-
π€ Model: `Zephyr-7B-Beta` Β· π Free via HuggingFace
|
| 212 |
-
π Get token: [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
|
| 213 |
-
""")
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
MODEL_ID = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 7 |
+
|
| 8 |
+
print("Loading model...")
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
+
pipe = pipeline(
|
| 11 |
+
"text-generation",
|
| 12 |
+
model=MODEL_ID,
|
| 13 |
+
torch_dtype=torch.float32,
|
| 14 |
+
device_map="auto"
|
| 15 |
+
)
|
| 16 |
+
print("Model loaded.")
|
| 17 |
+
|
| 18 |
+
SYSTEM = """You are an expert API architect. When given a product idea, recommend the best API stack with pricing and build order."""
|
| 19 |
+
|
| 20 |
+
def generate(prompt):
|
| 21 |
+
messages = [
|
| 22 |
+
{"role": "system", "content": SYSTEM},
|
| 23 |
+
{"role": "user", "content": prompt}
|
| 24 |
+
]
|
| 25 |
+
formatted = tokenizer.apply_chat_template(
|
| 26 |
+
messages,
|
| 27 |
+
tokenize=False,
|
| 28 |
+
add_generation_prompt=True
|
| 29 |
+
)
|
| 30 |
+
out = pipe(
|
| 31 |
+
formatted,
|
| 32 |
+
max_new_tokens=800,
|
| 33 |
+
do_sample=True,
|
| 34 |
+
temperature=0.7,
|
| 35 |
+
return_full_text=False
|
| 36 |
+
)
|
| 37 |
+
return out[0]["generated_text"].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
|
| 40 |
def get_stack(idea, budget, level, market):
|
| 41 |
if not idea.strip():
|
| 42 |
return "β οΈ Please describe your product idea."
|
| 43 |
+
try:
|
| 44 |
+
prompt = f"""Product idea: {idea}
|
| 45 |
+
Budget: {budget} | Level: {level} | Market: {market}
|
| 46 |
|
| 47 |
+
Recommend 3-5 APIs using this format:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
## π― Idea Summary
|
| 50 |
+
(one line)
|
| 51 |
|
| 52 |
+
## π§ API Stack
|
| 53 |
+
### [API Name] β [role]
|
| 54 |
+
- Pricing: (free tier + paid)
|
| 55 |
- Docs: (URL)
|
| 56 |
+
- Difficulty: Easy/Medium/Hard
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
## π° Cost Estimate
|
| 59 |
- 100 users: $X/month
|
| 60 |
- 1,000 users: $X/month
|
|
|
|
| 61 |
|
| 62 |
## β‘ Build Order
|
| 63 |
+
1. First: [API]
|
| 64 |
+
2. Second: [API]
|
|
|
|
| 65 |
|
| 66 |
+
## π¨ Top Mistake
|
| 67 |
(one paragraph)"""
|
| 68 |
+
return generate(prompt)
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return f"β Error: {str(e)}"
|
|
|
|
|
|
|
| 71 |
|
| 72 |
|
| 73 |
def ask_followup(question, prev):
|
| 74 |
if not question.strip():
|
| 75 |
return ""
|
| 76 |
if not prev or prev.startswith("β οΈ") or prev.startswith("β"):
|
| 77 |
+
return "β οΈ Generate a recommendation first."
|
| 78 |
+
try:
|
| 79 |
+
prompt = f"Previous recommendation:\n{prev[:500]}\n\nFollow-up: {question}\n\nAnswer concisely."
|
| 80 |
+
return generate(prompt)
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return f"β Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
EXAMPLES = [
|
| 86 |
+
["A notes app with AI summarization", "$50/month", "Intermediate", "Students"],
|
| 87 |
+
["SMS marketing for restaurants", "$30/month", "Beginner", "Restaurant owners"],
|
| 88 |
+
["AI job matching platform", "$100/month", "Advanced", "Tech recruiters"],
|
| 89 |
+
["Podcast transcription tool", "$20/month", "Beginner", "Podcasters"],
|
| 90 |
+
["Crypto portfolio tracker", "$0 (free only)", "Intermediate", "Retail investors"],
|
| 91 |
]
|
| 92 |
|
| 93 |
css = """
|
|
|
|
| 98 |
with gr.Blocks(css=css, title="API Stack Finder") as demo:
|
| 99 |
gr.Markdown("""
|
| 100 |
# π§ API Stack Finder
|
| 101 |
+
**Describe your product idea β Get the perfect API stack**
|
| 102 |
|
| 103 |
+
Powered by **TinyLlama** β runs locally inside this Space, no external API needed.
|
| 104 |
""")
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column(scale=3):
|
| 108 |
idea_input = gr.Textbox(
|
| 109 |
label="Describe your product idea",
|
| 110 |
+
placeholder="e.g. A freelancer marketplace with instant payments...",
|
| 111 |
lines=4
|
| 112 |
)
|
| 113 |
with gr.Column(scale=1):
|
| 114 |
budget_input = gr.Dropdown(
|
| 115 |
+
choices=["$0 (free only)", "$10/month", "$30/month",
|
| 116 |
+
"$50/month", "$100/month", "$500/month", "Unlimited"],
|
|
|
|
|
|
|
| 117 |
value="$30/month",
|
| 118 |
label="Monthly API budget"
|
| 119 |
)
|
|
|
|
| 131 |
gr.Markdown("**Try an example:**")
|
| 132 |
with gr.Row():
|
| 133 |
for ex in EXAMPLES:
|
| 134 |
+
gr.Button(ex[0][:30] + "...", size="sm").click(
|
| 135 |
lambda e=ex: (e[0], e[1], e[2], e[3]),
|
| 136 |
outputs=[idea_input, budget_input, level_input, market_input]
|
| 137 |
)
|
|
|
|
| 141 |
|
| 142 |
gr.Markdown("---")
|
| 143 |
gr.Markdown("### π¬ Ask a follow-up")
|
|
|
|
| 144 |
with gr.Row():
|
| 145 |
followup_input = gr.Textbox(
|
| 146 |
label="Follow-up question",
|
|
|
|
| 148 |
scale=4
|
| 149 |
)
|
| 150 |
followup_btn = gr.Button("Ask", scale=1, variant="secondary")
|
|
|
|
| 151 |
followup_output = gr.Markdown()
|
| 152 |
|
| 153 |
+
submit_btn.click(get_stack,
|
|
|
|
| 154 |
inputs=[idea_input, budget_input, level_input, market_input],
|
| 155 |
+
outputs=output)
|
| 156 |
+
followup_btn.click(ask_followup,
|
|
|
|
|
|
|
| 157 |
inputs=[followup_input, output],
|
| 158 |
+
outputs=followup_output)
|
|
|
|
| 159 |
|
| 160 |
+
gr.Markdown("---\nπ€ Model: `TinyLlama-1.1B-Chat` Β· Runs fully inside HuggingFace Space")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
if __name__ == "__main__":
|
| 163 |
demo.launch()
|