File size: 5,999 Bytes
0132878
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
import gradio as gr
from huggingface_hub import InferenceClient

# ----------------------
# TOKEN HANDLING (COLAB + SPACES)
# ----------------------
try:
    from google.colab import userdata
    HF_TOKEN = userdata.get("HF_TOKEN")   # Colab
except:
    HF_TOKEN = os.getenv("HF_TOKEN")      # Spaces

if not HF_TOKEN:
    raise ValueError("HF_TOKEN not found")

# ----------------------
# MODELS
# ----------------------
GEN_MODEL = "tiiuae/falcon-7b-instruct"
SUM_MODEL = "google/flan-t5-large"

client = InferenceClient(token=HF_TOKEN)

# ----------------------
# BACKEND FUNCTIONS
# ----------------------
def generate_tweet(topic, tone, max_length, history):
    if not topic.strip():
        return "⚠️ Please enter a topic.", history

    prompt = f"""
You are a world-class viral Twitter copywriter.

Write 3 highly engaging tweets about: {topic}

Rules:
- Tone: {tone}
- Strong hook
- Human-like writing
- Max 280 characters each
- Add 1-2 relevant hashtags
- Make them scroll-stopping
"""

    try:
        response = client.text_generation(
            model=GEN_MODEL,
            prompt=prompt,
            max_new_tokens=max_length,
            temperature=0.8
        )
        result = response.strip()
        history.append(f"Tweet | {topic}\n{result}\n")
        return result, history
    except Exception as e:
        return f"❌ Error: {str(e)}", history


def generate_blog_ideas(topic, tone, max_length, history):
    if not topic.strip():
        return "⚠️ Please enter a topic.", history

    prompt = f"""
You are an expert SEO strategist.

Generate 5 blog ideas about: {topic}

Rules:
- Tone: {tone}
- Specific and niche-focused
- Clear audience
- Click-worthy titles
"""

    try:
        response = client.text_generation(
            model=GEN_MODEL,
            prompt=prompt,
            max_new_tokens=max_length,
            temperature=0.7
        )
        result = response.strip()
        history.append(f"Blog | {topic}\n{result}\n")
        return result, history
    except Exception as e:
        return f"❌ Error: {str(e)}", history


def summarize_text(text, max_length, history):
    if not text.strip():
        return "⚠️ Please enter text.", history

    prompt = f"""
Summarize into:
- Bullet points
- Key insights
- Actionable takeaways

{text}
"""

    try:
        response = client.text_generation(
            model=SUM_MODEL,
            prompt=prompt,
            max_new_tokens=max_length,
            temperature=0.5
        )
        result = response.strip()
        history.append(f"Summary\n{result}\n")
        return result, history
    except Exception as e:
        return f"❌ Error: {str(e)}", history


def copy_text(text):
    return gr.update(value=text)

def show_history(history):
    return "\n".join(history)

# ----------------------
# UI
# ----------------------
with gr.Blocks(title="AI Productivity Assistant Pro") as app:

    history_state = gr.State([])

    gr.Markdown("# πŸ€– AI Productivity Assistant Pro")
    gr.Markdown("### Generate high-quality content in seconds")

    with gr.Tabs():

        # Tweet Generator
        with gr.Tab("🐦 Tweet Generator"):
            gr.Markdown("Generate viral tweets instantly")

            with gr.Row():
                with gr.Column():
                    topic = gr.Textbox(label="Topic")
                    tone = gr.Dropdown(
                        ["Professional", "Casual", "Funny", "Persuasive"],
                        value="Professional",
                        label="Tone"
                    )
                    max_len = gr.Slider(50, 300, value=150, label="Max Length")
                    btn = gr.Button("Generate")

                with gr.Column():
                    output = gr.Textbox(lines=10, label="Output")
                    copy_btn = gr.Button("Copy Output")

            btn.click(generate_tweet, [topic, tone, max_len, history_state], [output, history_state])
            copy_btn.click(copy_text, output, output)

        # Blog Ideas
        with gr.Tab("πŸ“ Blog Ideas"):
            gr.Markdown("Generate high-converting blog ideas")

            with gr.Row():
                with gr.Column():
                    topic_b = gr.Textbox(label="Topic")
                    tone_b = gr.Dropdown(
                        ["Professional", "Casual", "Funny", "Persuasive"],
                        value="Professional",
                        label="Tone"
                    )
                    max_len_b = gr.Slider(100, 400, value=200, label="Max Length")
                    btn_b = gr.Button("Generate")

                with gr.Column():
                    output_b = gr.Textbox(lines=10, label="Output")
                    copy_btn_b = gr.Button("Copy Output")

            btn_b.click(generate_blog_ideas, [topic_b, tone_b, max_len_b, history_state], [output_b, history_state])
            copy_btn_b.click(copy_text, output_b, output_b)

        # Summarizer
        with gr.Tab("πŸ“„ Summarizer"):
            gr.Markdown("Summarize text into insights")

            with gr.Row():
                with gr.Column():
                    text = gr.Textbox(lines=8, label="Text")
                    max_len_s = gr.Slider(50, 300, value=150, label="Max Length")
                    btn_s = gr.Button("Summarize")

                with gr.Column():
                    output_s = gr.Textbox(lines=10, label="Output")
                    copy_btn_s = gr.Button("Copy Output")

            btn_s.click(summarize_text, [text, max_len_s, history_state], [output_s, history_state])
            copy_btn_s.click(copy_text, output_s, output_s)

        # History
        with gr.Tab("πŸ“œ History"):
            history_box = gr.Textbox(lines=20, label="History")
            refresh_btn = gr.Button("Refresh")
            refresh_btn.click(show_history, history_state, history_box)

    gr.Markdown("---")
    gr.Markdown("Built with Hugging Face | AI SaaS Prototype")

app.launch(share=True)