phxdev commited on
Commit
166bd18
Β·
verified Β·
1 Parent(s): 9699f3b

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +63 -60
app.py CHANGED
@@ -9,18 +9,25 @@ generator = None
9
  def initialize_model():
10
  global generator
11
  try:
12
- # Start with Qwen 2.5-7B-Instruct (more reliable than Omni for Spaces)
13
- device = 0 if torch.cuda.is_available() else -1
14
- generator = pipeline(
15
- "text-generation",
16
- model="Qwen/Qwen2.5-7B-Instruct",
17
- device=device,
18
- torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
19
- trust_remote_code=True
20
- )
21
- return f"Qwen 2.5-7B-Instruct loaded successfully on {'GPU' if device == 0 else 'CPU'}!"
 
 
 
 
 
 
 
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 "Fallback model (Qwen 2.5-1.5B-Instruct) loaded on CPU!"
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 "Final fallback model (DialoGPT-large) loaded on CPU!"
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 Qwen 2.5 instruction format
55
  prompt = f"""<|im_start|>system
56
- You are a professional document writer specializing in creating concise, well-structured one-page business documents.
57
  <|im_end|>
58
  <|im_start|>user
59
- Create a professional one-page document about "{topic}" targeted at {target_audience}.
60
 
61
- Requirements:
62
- - Tone: {tone.lower()}
63
- - Key points to include: {key_points}
64
- - Length: {length}
65
- - Format: Use clear headers and bullet points
66
- - Structure: Title, Executive Summary, Key Points, Benefits, Recommendations, Conclusion
67
 
68
- Please write the complete one-page document now.
 
 
 
 
 
 
 
69
  <|im_end|>
70
  <|im_start|>assistant
71
- # {topic}
 
72
 
73
- ## Executive Summary
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
- tone_styles = {
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
- template = f"""# {topic}
120
-
121
- ## Executive Summary
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
- {chr(10).join([f"β€’ {point.strip()}" for point in key_points.split(',') if point.strip()])}
127
 
128
- ## Background
129
- {topic} represents an important area that requires careful consideration and strategic thinking. Understanding the core concepts and implications is essential for {target_audience.lower()}.
130
 
131
- ## Main Content
132
- The fundamental aspects of {topic.lower()} encompass several critical areas that directly impact stakeholders. These elements work together to create a comprehensive framework for understanding and implementation.
133
 
134
- ## Benefits & Opportunities
135
- - Enhanced understanding of core concepts
136
- - Improved decision-making capabilities
137
- - Strategic advantages for implementation
138
- - Clear actionable insights
139
 
140
- ## Recommendations
141
- 1. Begin with thorough analysis of current situation
142
- 2. Develop comprehensive implementation strategy
143
- 3. Monitor progress and adjust approach as needed
144
- 4. Measure results and iterate for continuous improvement
145
 
146
- ## Conclusion
147
- {topic} offers significant opportunities for {target_audience.lower()} when approached strategically. The key points outlined above provide a solid foundation for moving forward with confidence and clarity.
 
148
 
149
- ---
150
- *This one-pager was generated to provide quick, actionable insights on {topic.lower()}.*"""
 
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 professional one-page documents on any topic using AI!")
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):