Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,27 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# Initialize OpenAI client
|
| 6 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
if not api_key:
|
| 8 |
-
raise ValueError("OPENAI_API_KEY not found in environment variables.")
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
client = OpenAI(api_key=api_key)
|
| 11 |
|
| 12 |
def generate_business_concept(item1, item2, market1, market2):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if not all([item1, item2, market1, market2]):
|
| 14 |
return "Please provide all four inputs: two items and two target markets."
|
| 15 |
|
|
|
|
| 16 |
system_prompt = """You are a business concept generator. When given exactly two items and two target markets, you must:
|
| 17 |
|
| 18 |
1. Create exactly two sentences describing a business that uses or sells both items as a coherent product or service.
|
|
@@ -24,9 +33,11 @@ Output format:
|
|
| 24 |
|
| 25 |
Provide no additional commentary, explanations, or text beyond these three sentences."""
|
| 26 |
|
|
|
|
| 27 |
user_message = f"Items: {item1}, {item2}\nTarget Markets: {market1}, {market2}"
|
| 28 |
|
| 29 |
try:
|
|
|
|
| 30 |
response = client.chat.completions.create(
|
| 31 |
model="gpt-3.5-turbo",
|
| 32 |
messages=[
|
|
@@ -36,28 +47,151 @@ Provide no additional commentary, explanations, or text beyond these three sente
|
|
| 36 |
temperature=0.7,
|
| 37 |
max_tokens=200
|
| 38 |
)
|
|
|
|
| 39 |
return response.choices[0].message.content.strip()
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error generating concept: {str(e)}"
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
gr.
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
from openai import OpenAI
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Initialize OpenAI client - for Hugging Face Spaces
|
| 7 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
if not api_key:
|
| 9 |
+
raise ValueError("OPENAI_API_KEY not found in environment variables. Please set it in the Space secrets.")
|
| 10 |
+
|
| 11 |
+
# Debug: Print first few characters to verify format (remove this after testing)
|
| 12 |
+
print(f"API Key format check: {api_key[:10]}...")
|
| 13 |
|
| 14 |
client = OpenAI(api_key=api_key)
|
| 15 |
|
| 16 |
def generate_business_concept(item1, item2, market1, market2):
|
| 17 |
+
"""
|
| 18 |
+
Generate a business concept using two items and two target markets
|
| 19 |
+
"""
|
| 20 |
+
# Validate inputs
|
| 21 |
if not all([item1, item2, market1, market2]):
|
| 22 |
return "Please provide all four inputs: two items and two target markets."
|
| 23 |
|
| 24 |
+
# System prompt that defines the GPT's behavior
|
| 25 |
system_prompt = """You are a business concept generator. When given exactly two items and two target markets, you must:
|
| 26 |
|
| 27 |
1. Create exactly two sentences describing a business that uses or sells both items as a coherent product or service.
|
|
|
|
| 33 |
|
| 34 |
Provide no additional commentary, explanations, or text beyond these three sentences."""
|
| 35 |
|
| 36 |
+
# User message with the inputs
|
| 37 |
user_message = f"Items: {item1}, {item2}\nTarget Markets: {market1}, {market2}"
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
# Call OpenAI API
|
| 41 |
response = client.chat.completions.create(
|
| 42 |
model="gpt-3.5-turbo",
|
| 43 |
messages=[
|
|
|
|
| 47 |
temperature=0.7,
|
| 48 |
max_tokens=200
|
| 49 |
)
|
| 50 |
+
|
| 51 |
return response.choices[0].message.content.strip()
|
| 52 |
+
|
| 53 |
except Exception as e:
|
| 54 |
return f"Error generating concept: {str(e)}"
|
| 55 |
|
| 56 |
+
# Create Gradio interface with mobile-first design
|
| 57 |
+
def create_interface():
|
| 58 |
+
with gr.Blocks(
|
| 59 |
+
title="Business Concept Generator",
|
| 60 |
+
theme=gr.themes.Default(),
|
| 61 |
+
css="""
|
| 62 |
+
/* Mobile-first CSS */
|
| 63 |
+
.gradio-container {
|
| 64 |
+
max-width: 100% !important;
|
| 65 |
+
padding: 10px !important;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
/* Force vertical layout on small screens */
|
| 69 |
+
@media (max-width: 480px) {
|
| 70 |
+
.gr-row {
|
| 71 |
+
flex-direction: column !important;
|
| 72 |
+
}
|
| 73 |
+
.gr-column {
|
| 74 |
+
width: 100% !important;
|
| 75 |
+
min-width: 100% !important;
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
/* Make inputs more touch-friendly */
|
| 80 |
+
input[type="text"], textarea {
|
| 81 |
+
font-size: 16px !important; /* Prevents zoom on iOS */
|
| 82 |
+
padding: 12px !important;
|
| 83 |
+
width: 100% !important;
|
| 84 |
+
box-sizing: border-box !important;
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
/* Style the button for mobile */
|
| 88 |
+
.gr-button {
|
| 89 |
+
width: 100% !important;
|
| 90 |
+
min-height: 50px !important;
|
| 91 |
+
font-size: 18px !important;
|
| 92 |
+
font-weight: bold !important;
|
| 93 |
+
-webkit-appearance: none !important;
|
| 94 |
+
-moz-appearance: none !important;
|
| 95 |
+
appearance: none !important;
|
| 96 |
+
touch-action: manipulation !important;
|
| 97 |
+
margin: 10px 0 !important;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
/* Compact header */
|
| 101 |
+
h1 {
|
| 102 |
+
font-size: 24px !important;
|
| 103 |
+
margin: 10px 0 !important;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
/* Reduce spacing */
|
| 107 |
+
.gr-form {
|
| 108 |
+
gap: 10px !important;
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
/* Make textboxes more compact */
|
| 112 |
+
.gr-textbox {
|
| 113 |
+
margin-bottom: 10px !important;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
/* Hide examples on very small screens */
|
| 117 |
+
@media (max-width: 375px) {
|
| 118 |
+
.gr-examples {
|
| 119 |
+
display: none !important;
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
"""
|
| 123 |
+
) as demo:
|
| 124 |
+
gr.Markdown("""
|
| 125 |
+
# Business Concept Generator
|
| 126 |
+
|
| 127 |
+
Generate business ideas from 2 items & 2 markets.
|
| 128 |
+
""")
|
| 129 |
+
|
| 130 |
+
# Use single column layout for mobile
|
| 131 |
+
with gr.Column():
|
| 132 |
+
item1 = gr.Textbox(
|
| 133 |
+
label="Item 1",
|
| 134 |
+
placeholder="First item",
|
| 135 |
+
lines=1,
|
| 136 |
+
max_lines=1
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
item2 = gr.Textbox(
|
| 140 |
+
label="Item 2",
|
| 141 |
+
placeholder="Second item",
|
| 142 |
+
lines=1,
|
| 143 |
+
max_lines=1
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
market1 = gr.Textbox(
|
| 147 |
+
label="Target Market 1",
|
| 148 |
+
placeholder="First market",
|
| 149 |
+
lines=1,
|
| 150 |
+
max_lines=1
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
market2 = gr.Textbox(
|
| 154 |
+
label="Target Market 2",
|
| 155 |
+
placeholder="Second market",
|
| 156 |
+
lines=1,
|
| 157 |
+
max_lines=1
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
generate_btn = gr.Button(
|
| 161 |
+
"Generate Concept",
|
| 162 |
+
variant="primary",
|
| 163 |
+
elem_classes=["generate-button"]
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
output = gr.Textbox(
|
| 167 |
+
label="Business Concept",
|
| 168 |
+
lines=3,
|
| 169 |
+
max_lines=5,
|
| 170 |
+
interactive=False
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# Handle button click
|
| 174 |
+
generate_btn.click(
|
| 175 |
+
fn=generate_business_concept,
|
| 176 |
+
inputs=[item1, item2, market1, market2],
|
| 177 |
+
outputs=output
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
# Compact examples
|
| 181 |
+
with gr.Accordion("Examples", open=False):
|
| 182 |
+
gr.Examples(
|
| 183 |
+
examples=[
|
| 184 |
+
["Coffee", "Books", "Students", "Remote Workers"],
|
| 185 |
+
["Bikes", "Solar", "Commuters", "Eco-owners"]
|
| 186 |
+
],
|
| 187 |
+
inputs=[item1, item2, market1, market2],
|
| 188 |
+
label=""
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
return demo
|
| 192 |
|
| 193 |
+
# Main execution
|
| 194 |
if __name__ == "__main__":
|
| 195 |
+
# Create and launch the interface
|
| 196 |
+
demo = create_interface()
|
| 197 |
+
demo.launch()
|