phxdev commited on
Commit
53cb344
·
verified ·
1 Parent(s): 51d8aa9

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +39 -13
app.py CHANGED
@@ -9,16 +9,38 @@ generator = None
9
  def initialize_model():
10
  global generator
11
  try:
12
- # Use a reliable text generation model
13
  generator = pipeline(
14
  "text-generation",
15
- model="distilgpt2", # Smaller, faster model
16
  device=-1, # CPU only for compatibility
17
- pad_token_id=50256 # Set pad token to avoid warnings
 
18
  )
19
  return "Model loaded successfully!"
20
  except Exception as e:
21
- return f"Error loading model: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  def generate_onepager(topic, target_audience, key_points, tone, length):
24
  if generator is None:
@@ -28,20 +50,24 @@ def generate_onepager(topic, target_audience, key_points, tone, length):
28
  length_tokens = {"Short": 200, "Medium": 400, "Long": 600}
29
  max_tokens = length_tokens.get(length, 400)
30
 
31
- prompt = f"""ONE-PAGE DOCUMENT
 
32
 
33
- TOPIC: {topic}
34
- AUDIENCE: {target_audience}
35
- TONE: {tone}
36
 
37
- EXECUTIVE SUMMARY:
38
- {key_points}
 
 
 
 
39
 
40
- DETAILED CONTENT:
41
 
42
- Title: {topic}
43
 
44
- Overview:"""
 
45
 
46
  try:
47
  # Generate the one-pager
 
9
  def initialize_model():
10
  global generator
11
  try:
12
+ # Use a modern instruction-tuned model for better document generation
13
  generator = pipeline(
14
  "text-generation",
15
+ model="microsoft/phi-2", # Small but very capable instruction model
16
  device=-1, # CPU only for compatibility
17
+ torch_dtype=torch.float32,
18
+ trust_remote_code=True
19
  )
20
  return "Model loaded successfully!"
21
  except Exception as e:
22
+ # Fallback to Flan-T5 for instruction following
23
+ try:
24
+ from transformers import T5ForConditionalGeneration, T5Tokenizer
25
+ generator = pipeline(
26
+ "text2text-generation",
27
+ model="google/flan-t5-base",
28
+ device=-1,
29
+ torch_dtype=torch.float32
30
+ )
31
+ return "Fallback model (Flan-T5-base) loaded successfully!"
32
+ except Exception as e2:
33
+ # Final fallback to GPT2-medium
34
+ try:
35
+ generator = pipeline(
36
+ "text-generation",
37
+ model="gpt2-medium",
38
+ device=-1,
39
+ torch_dtype=torch.float32
40
+ )
41
+ return "Final fallback model (GPT2-medium) loaded successfully!"
42
+ except Exception as e3:
43
+ return f"All models failed: Primary: {str(e)}, Secondary: {str(e2)}, Tertiary: {str(e3)}"
44
 
45
  def generate_onepager(topic, target_audience, key_points, tone, length):
46
  if generator is None:
 
50
  length_tokens = {"Short": 200, "Medium": 400, "Long": 600}
51
  max_tokens = length_tokens.get(length, 400)
52
 
53
+ # Create a better prompt for modern instruction-following models
54
+ prompt = f"""Write a professional one-page document about "{topic}" for {target_audience}.
55
 
56
+ Use a {tone.lower()} tone and include these key points: {key_points}
 
 
57
 
58
+ Format the document with:
59
+ - Clear title
60
+ - Executive summary
61
+ - Main sections with headers
62
+ - Key benefits/recommendations
63
+ - Conclusion
64
 
65
+ Document:
66
 
67
+ # {topic}
68
 
69
+ ## Executive Summary
70
+ """
71
 
72
  try:
73
  # Generate the one-pager