phxdev commited on
Commit
fc1df26
Β·
verified Β·
1 Parent(s): 023f026

Upload requirements.txt with huggingface_hub

Browse files
Files changed (1) hide show
  1. requirements.txt +239 -6
requirements.txt CHANGED
@@ -1,6 +1,239 @@
1
- torch
2
- transformers
3
- gradio
4
- huggingface_hub
5
- tokenizers
6
- accelerate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+ import os
5
+
6
+ # Initialize the text generation pipeline
7
+ generator = None
8
+
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",
34
+ model="Qwen/Qwen2.5-1.5B-Instruct",
35
+ device=-1,
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:
43
+ generator = pipeline(
44
+ "text-generation",
45
+ model="microsoft/DialoGPT-large",
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
+
53
+ def generate_onepager(topic, target_audience, key_points, tone, length):
54
+ if generator is None:
55
+ return "Error: Model not initialized. Please wait for the model to load."
56
+
57
+ # Create a structured prompt for one-pager generation
58
+ length_tokens = {"Short": 200, "Medium": 400, "Long": 600}
59
+ max_tokens = length_tokens.get(length, 400)
60
+
61
+ # Create an optimized prompt for Qwen 2.5 instruction format
62
+ prompt = f"""<|im_start|>system
63
+ You are a professional document writer specializing in creating concise, well-structured one-page business documents.
64
+ <|im_end|>
65
+ <|im_start|>user
66
+ Create a professional one-page document about "{topic}" targeted at {target_audience}.
67
+
68
+ Requirements:
69
+ - Tone: {tone.lower()}
70
+ - Key points to include: {key_points}
71
+ - Length: {length}
72
+ - Format: Use clear headers and bullet points
73
+ - Structure: Title, Executive Summary, Key Points, Benefits, Recommendations, Conclusion
74
+
75
+ Please write the complete one-page document now.
76
+ <|im_end|>
77
+ <|im_start|>assistant
78
+ # {topic}
79
+
80
+ ## Executive Summary
81
+
82
+ """
83
+
84
+ try:
85
+ # Generate the one-pager
86
+ result = generator(
87
+ prompt,
88
+ max_length=len(prompt.split()) + max_tokens,
89
+ num_return_sequences=1,
90
+ temperature=0.8,
91
+ do_sample=True,
92
+ pad_token_id=generator.tokenizer.eos_token_id,
93
+ eos_token_id=generator.tokenizer.eos_token_id,
94
+ repetition_penalty=1.1
95
+ )
96
+
97
+ # Extract the generated text
98
+ generated_text = result[0]['generated_text']
99
+
100
+ # Clean up the output
101
+ onepager = generated_text.replace(prompt, "").strip()
102
+
103
+ # If output is too short, provide a structured fallback
104
+ if len(onepager) < 50:
105
+ onepager = create_structured_onepager(topic, target_audience, key_points, tone)
106
+
107
+ return onepager
108
+
109
+ except Exception as e:
110
+ # Fallback to structured template
111
+ return create_structured_onepager(topic, target_audience, key_points, tone)
112
+
113
+ def create_structured_onepager(topic, target_audience, key_points, tone):
114
+ """Create a structured one-pager when AI generation fails"""
115
+
116
+ tone_styles = {
117
+ "Professional": "formal and business-oriented",
118
+ "Casual": "friendly and approachable",
119
+ "Academic": "scholarly and research-focused",
120
+ "Persuasive": "compelling and action-oriented",
121
+ "Informative": "clear and educational"
122
+ }
123
+
124
+ style_desc = tone_styles.get(tone, "professional")
125
+
126
+ template = f"""# {topic}
127
+
128
+ ## Executive Summary
129
+ 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.
130
+
131
+ ## Key Points
132
+
133
+ {chr(10).join([f"β€’ {point.strip()}" for point in key_points.split(',') if point.strip()])}
134
+
135
+ ## Background
136
+ {topic} represents an important area that requires careful consideration and strategic thinking. Understanding the core concepts and implications is essential for {target_audience.lower()}.
137
+
138
+ ## Main Content
139
+ 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.
140
+
141
+ ## Benefits & Opportunities
142
+ - Enhanced understanding of core concepts
143
+ - Improved decision-making capabilities
144
+ - Strategic advantages for implementation
145
+ - Clear actionable insights
146
+
147
+ ## Recommendations
148
+ 1. Begin with thorough analysis of current situation
149
+ 2. Develop comprehensive implementation strategy
150
+ 3. Monitor progress and adjust approach as needed
151
+ 4. Measure results and iterate for continuous improvement
152
+
153
+ ## Conclusion
154
+ {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.
155
+
156
+ ---
157
+ *This one-pager was generated to provide quick, actionable insights on {topic.lower()}.*"""
158
+
159
+ return template
160
+
161
+ # Create the Gradio interface
162
+ def create_interface():
163
+ with gr.Blocks(title="One-Pager Generator", theme=gr.themes.Soft()) as demo:
164
+ gr.Markdown("# πŸ“„ AI One-Pager Generator")
165
+ gr.Markdown("Generate professional one-page documents on any topic using AI!")
166
+
167
+ with gr.Row():
168
+ with gr.Column(scale=1):
169
+ topic_input = gr.Textbox(
170
+ label="Topic",
171
+ placeholder="e.g., Digital Marketing Strategy, Climate Change Solutions, etc.",
172
+ lines=2,
173
+ value="Artificial Intelligence in Healthcare"
174
+ )
175
+
176
+ audience_input = gr.Textbox(
177
+ label="Target Audience",
178
+ placeholder="e.g., Business executives, Students, General public, etc.",
179
+ lines=1,
180
+ value="Healthcare professionals"
181
+ )
182
+
183
+ keypoints_input = gr.Textbox(
184
+ label="Key Points to Cover",
185
+ placeholder="Enter main points separated by commas",
186
+ lines=4,
187
+ value="Machine learning applications, Data privacy, Cost-effectiveness, Implementation challenges"
188
+ )
189
+
190
+ tone_dropdown = gr.Dropdown(
191
+ choices=["Professional", "Casual", "Academic", "Persuasive", "Informative"],
192
+ label="Tone",
193
+ value="Professional"
194
+ )
195
+
196
+ length_dropdown = gr.Dropdown(
197
+ choices=["Short", "Medium", "Long"],
198
+ label="Length",
199
+ value="Medium"
200
+ )
201
+
202
+ generate_btn = gr.Button("πŸš€ Generate One-Pager", variant="primary")
203
+
204
+ with gr.Column(scale=2):
205
+ output_text = gr.Textbox(
206
+ label="Generated One-Pager",
207
+ lines=25,
208
+ max_lines=35,
209
+ show_copy_button=True,
210
+ placeholder="Your generated one-pager will appear here..."
211
+ )
212
+
213
+ with gr.Row():
214
+ gr.Markdown("""
215
+ ### πŸ’‘ Tips for Best Results:
216
+ - **Be specific** with your topic for more targeted content
217
+ - **Include 3-5 key points** separated by commas
218
+ - **Choose the right tone** for your intended audience
219
+ - **Use descriptive audience** details (e.g., "C-level executives" vs "executives")
220
+ """)
221
+
222
+ # Connect the generate button to the function
223
+ generate_btn.click(
224
+ fn=generate_onepager,
225
+ inputs=[topic_input, audience_input, keypoints_input, tone_dropdown, length_dropdown],
226
+ outputs=output_text
227
+ )
228
+
229
+ return demo
230
+
231
+ # Initialize model and launch
232
+ if __name__ == "__main__":
233
+ print("πŸš€ Starting One-Pager Generator with Qwen 2.5-7B...")
234
+ print("πŸ“₯ Loading AI model...")
235
+ initialize_model()
236
+ print("βœ… Model loaded! Launching interface...")
237
+
238
+ demo = create_interface()
239
+ demo.launch()