Spaces:
Sleeping
Sleeping
Pooja P
commited on
Commit
·
f96f50a
1
Parent(s):
d5c17b0
added new app1.py
Browse files
app1.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from transformers import pipeline
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load token from environment variables (make sure to set it in Hugging Face Secrets)
|
6 |
+
token = os.environ.get("OPENAI_API_KEY")
|
7 |
+
|
8 |
+
# Use a lightweight model to avoid memory/time limit errors
|
9 |
+
generator = pipeline("text-generation", model="tiiuae/falcon-rw-1b", token=token)
|
10 |
+
|
11 |
+
def clean_topic(topic):
|
12 |
+
topic = topic.lower()
|
13 |
+
if "write a blog on" in topic:
|
14 |
+
topic = topic.replace("write a blog on", "").strip()
|
15 |
+
elif "write a blog about" in topic:
|
16 |
+
topic = topic.replace("write a blog about", "").strip()
|
17 |
+
return topic.capitalize()
|
18 |
+
|
19 |
+
def generate_blog(topic):
|
20 |
+
topic = clean_topic(topic)
|
21 |
+
|
22 |
+
prompt = f"""
|
23 |
+
You are a helpful blog writer assistant.
|
24 |
+
|
25 |
+
Write a complete and engaging blog post on the topic: "{topic}"
|
26 |
+
|
27 |
+
Format:
|
28 |
+
- Title
|
29 |
+
- Introduction
|
30 |
+
- 2 to 3 subheadings with 1-2 paragraphs each
|
31 |
+
- Conclusion
|
32 |
+
|
33 |
+
Tone: Friendly, informative, and conversational. Keep it easy to understand for general readers. Use markdown for structure (like ## for subheadings).
|
34 |
+
|
35 |
+
Example:
|
36 |
+
|
37 |
+
Title: The Power of Renewable Energy
|
38 |
+
|
39 |
+
Introduction:
|
40 |
+
Imagine a world where our homes and cities run on sunlight, wind, and water. Renewable energy isn't just a buzzword—it’s the solution to a cleaner, greener planet.
|
41 |
+
|
42 |
+
## What is Renewable Energy?
|
43 |
+
|
44 |
+
...
|
45 |
+
|
46 |
+
Now write a similar blog on: "{topic}"
|
47 |
+
"""
|
48 |
+
|
49 |
+
result = generator(prompt, max_length=800, do_sample=True, temperature=0.7, top_p=0.9)
|
50 |
+
|
51 |
+
try:
|
52 |
+
return result[0]['generated_text']
|
53 |
+
except (KeyError, IndexError):
|
54 |
+
return "❌ Failed to generate blog. Please try again."
|
55 |
+
|
56 |
+
# Create Gradio interface
|
57 |
+
gr.Interface(
|
58 |
+
fn=generate_blog,
|
59 |
+
inputs=gr.Textbox(placeholder="Enter a topic like 'Clean Energy'", label="Blog Topic"),
|
60 |
+
outputs=gr.Textbox(label="Generated Blog"),
|
61 |
+
title="📝 AI Blog Writer",
|
62 |
+
description="Generate a friendly and informative blog post on any topic.",
|
63 |
+
).launch(share=True)
|