Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -9,18 +9,25 @@ generator = None
|
|
| 9 |
def initialize_model():
|
| 10 |
global generator
|
| 11 |
try:
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
device=
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
-
# Fallback to 1.5B model
|
| 24 |
try:
|
| 25 |
generator = pipeline(
|
| 26 |
"text-generation",
|
|
@@ -29,7 +36,7 @@ def initialize_model():
|
|
| 29 |
torch_dtype=torch.float32,
|
| 30 |
trust_remote_code=True
|
| 31 |
)
|
| 32 |
-
return "
|
| 33 |
except Exception as e2:
|
| 34 |
# Final fallback to a very reliable model
|
| 35 |
try:
|
|
@@ -39,7 +46,7 @@ def initialize_model():
|
|
| 39 |
device=-1,
|
| 40 |
torch_dtype=torch.float32
|
| 41 |
)
|
| 42 |
-
return "
|
| 43 |
except Exception as e3:
|
| 44 |
return f"All models failed: 7B: {str(e)}, 1.5B: {str(e2)}, DialoGPT: {str(e3)}"
|
| 45 |
|
|
@@ -51,26 +58,31 @@ def generate_onepager(topic, target_audience, key_points, tone, length):
|
|
| 51 |
length_tokens = {"Short": 200, "Medium": 400, "Long": 600}
|
| 52 |
max_tokens = length_tokens.get(length, 400)
|
| 53 |
|
| 54 |
-
# Create an optimized prompt for
|
| 55 |
prompt = f"""<|im_start|>system
|
| 56 |
-
You are a
|
| 57 |
<|im_end|>
|
| 58 |
<|im_start|>user
|
| 59 |
-
Create a
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
- Length: {length}
|
| 65 |
-
- Format: Use clear headers and bullet points
|
| 66 |
-
- Structure: Title, Executive Summary, Key Points, Benefits, Recommendations, Conclusion
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
<|im_end|>
|
| 70 |
<|im_start|>assistant
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
"""
|
| 76 |
|
|
@@ -106,48 +118,39 @@ Please write the complete one-page document now.
|
|
| 106 |
def create_structured_onepager(topic, target_audience, key_points, tone):
|
| 107 |
"""Create a structured one-pager when AI generation fails"""
|
| 108 |
|
| 109 |
-
|
| 110 |
-
"Professional": "formal and business-oriented",
|
| 111 |
-
"Casual": "friendly and approachable",
|
| 112 |
-
"Academic": "scholarly and research-focused",
|
| 113 |
-
"Persuasive": "compelling and action-oriented",
|
| 114 |
-
"Informative": "clear and educational"
|
| 115 |
-
}
|
| 116 |
-
|
| 117 |
-
style_desc = tone_styles.get(tone, "professional")
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
This document provides a comprehensive overview of {topic.lower()} tailored for {target_audience.lower()}. The content is presented in a {style_desc} manner to ensure maximum impact and understanding.
|
| 123 |
-
|
| 124 |
-
## Key Points
|
| 125 |
|
| 126 |
-
|
| 127 |
|
| 128 |
-
|
| 129 |
-
{topic}
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
|
| 140 |
-
|
| 141 |
-
1.
|
| 142 |
-
2.
|
| 143 |
-
3.
|
| 144 |
-
4.
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
|
|
|
| 151 |
|
| 152 |
return template
|
| 153 |
|
|
@@ -155,7 +158,7 @@ The fundamental aspects of {topic.lower()} encompass several critical areas that
|
|
| 155 |
def create_interface():
|
| 156 |
with gr.Blocks(title="One-Pager Generator", theme=gr.themes.Soft()) as demo:
|
| 157 |
gr.Markdown("# π AI One-Pager Generator")
|
| 158 |
-
gr.Markdown("Generate
|
| 159 |
|
| 160 |
with gr.Row():
|
| 161 |
with gr.Column(scale=1):
|
|
|
|
| 9 |
def initialize_model():
|
| 10 |
global generator
|
| 11 |
try:
|
| 12 |
+
# Check available memory and choose model accordingly
|
| 13 |
+
import psutil
|
| 14 |
+
available_memory_gb = psutil.virtual_memory().available / (1024**3)
|
| 15 |
+
|
| 16 |
+
if available_memory_gb > 20: # If we have enough memory, try 7B
|
| 17 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 18 |
+
generator = pipeline(
|
| 19 |
+
"text-generation",
|
| 20 |
+
model="Qwen/Qwen2.5-7B-Instruct",
|
| 21 |
+
device=device,
|
| 22 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 23 |
+
trust_remote_code=True
|
| 24 |
+
)
|
| 25 |
+
return f"Qwen 2.5-7B-Instruct loaded successfully on {'GPU' if device == 0 else 'CPU'}!"
|
| 26 |
+
else:
|
| 27 |
+
raise Exception("Insufficient memory for 7B model, falling back to 1.5B")
|
| 28 |
+
|
| 29 |
except Exception as e:
|
| 30 |
+
# Fallback to 1.5B model for free tier compatibility
|
| 31 |
try:
|
| 32 |
generator = pipeline(
|
| 33 |
"text-generation",
|
|
|
|
| 36 |
torch_dtype=torch.float32,
|
| 37 |
trust_remote_code=True
|
| 38 |
)
|
| 39 |
+
return "Qwen 2.5-1.5B-Instruct loaded successfully on CPU (optimized for free tier)!"
|
| 40 |
except Exception as e2:
|
| 41 |
# Final fallback to a very reliable model
|
| 42 |
try:
|
|
|
|
| 46 |
device=-1,
|
| 47 |
torch_dtype=torch.float32
|
| 48 |
)
|
| 49 |
+
return "DialoGPT-large loaded as final fallback!"
|
| 50 |
except Exception as e3:
|
| 51 |
return f"All models failed: 7B: {str(e)}, 1.5B: {str(e2)}, DialoGPT: {str(e3)}"
|
| 52 |
|
|
|
|
| 58 |
length_tokens = {"Short": 200, "Medium": 400, "Long": 600}
|
| 59 |
max_tokens = length_tokens.get(length, 400)
|
| 60 |
|
| 61 |
+
# Create an optimized prompt for actual one-pager format
|
| 62 |
prompt = f"""<|im_start|>system
|
| 63 |
+
You are a business consultant who creates compelling one-page documents that drive decisions and action. Your one-pagers are visual, concise, and immediately impactful.
|
| 64 |
<|im_end|>
|
| 65 |
<|im_start|>user
|
| 66 |
+
Create a compelling one-page business document about "{topic}" for {target_audience}.
|
| 67 |
|
| 68 |
+
Style: {tone.lower()} but action-oriented
|
| 69 |
+
Key points: {key_points}
|
| 70 |
+
Length: {length}
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
Format as a TRUE one-pager with:
|
| 73 |
+
- Bold title and clear audience
|
| 74 |
+
- Visual elements (bullets, emojis, sections)
|
| 75 |
+
- Concise benefits and value proposition
|
| 76 |
+
- Clear next steps and decision points
|
| 77 |
+
- Urgency and compelling call to action
|
| 78 |
+
|
| 79 |
+
Make it scannable, impactful, and designed to get approval/buy-in.
|
| 80 |
<|im_end|>
|
| 81 |
<|im_start|>assistant
|
| 82 |
+
{topic.upper()}
|
| 83 |
+
{'=' * len(topic)}
|
| 84 |
|
| 85 |
+
π― FOR: {target_audience.title()}
|
| 86 |
|
| 87 |
"""
|
| 88 |
|
|
|
|
| 118 |
def create_structured_onepager(topic, target_audience, key_points, tone):
|
| 119 |
"""Create a structured one-pager when AI generation fails"""
|
| 120 |
|
| 121 |
+
key_points_list = [point.strip() for point in key_points.split(',') if point.strip()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
# Create a more concise, visual one-pager format
|
| 124 |
+
template = f"""{topic.upper()}
|
| 125 |
+
{'=' * len(topic)}
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
π― FOR: {target_audience.title()}
|
| 128 |
|
| 129 |
+
π‘ WHAT IS IT?
|
| 130 |
+
{topic} is a strategic initiative that delivers measurable value through focused implementation and clear outcomes.
|
| 131 |
|
| 132 |
+
π KEY BENEFITS:
|
| 133 |
+
{chr(10).join([f" β {point.strip()}" for point in key_points_list[:4]])}
|
| 134 |
|
| 135 |
+
π THE OPPORTUNITY:
|
| 136 |
+
β’ Immediate impact on core business objectives
|
| 137 |
+
β’ Scalable solution with long-term benefits
|
| 138 |
+
β’ Competitive advantage in the market
|
| 139 |
+
β’ Risk mitigation and operational efficiency
|
| 140 |
|
| 141 |
+
π NEXT STEPS:
|
| 142 |
+
1. ASSESS current state and requirements
|
| 143 |
+
2. PLAN implementation strategy and timeline
|
| 144 |
+
3. EXECUTE with clear milestones and metrics
|
| 145 |
+
4. MEASURE results and optimize performance
|
| 146 |
|
| 147 |
+
π° INVESTMENT: Minimal upfront cost for maximum ROI
|
| 148 |
+
β±οΈ TIMELINE: Quick wins in 30-60 days, full benefits within 6 months
|
| 149 |
+
π SUCCESS METRICS: Improved efficiency, reduced costs, enhanced outcomes
|
| 150 |
|
| 151 |
+
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 152 |
+
DECISION POINT: Move forward with {topic.lower()} to capture these benefits
|
| 153 |
+
Contact: [Your Team] for implementation planning and support"""
|
| 154 |
|
| 155 |
return template
|
| 156 |
|
|
|
|
| 158 |
def create_interface():
|
| 159 |
with gr.Blocks(title="One-Pager Generator", theme=gr.themes.Soft()) as demo:
|
| 160 |
gr.Markdown("# π AI One-Pager Generator")
|
| 161 |
+
gr.Markdown("Generate compelling, decision-focused one-pagers that drive action and get buy-in!")
|
| 162 |
|
| 163 |
with gr.Row():
|
| 164 |
with gr.Column(scale=1):
|