Spaces:
Sleeping
Sleeping
Pooja P
commited on
Commit
·
bfe3b07
1
Parent(s):
f96f50a
added new app1.py with modification
Browse files
app1.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
import os
|
2 |
-
|
3 |
import gradio as gr
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
generator = pipeline("text-generation", model="tiiuae/falcon-rw-1b", token=token)
|
10 |
|
11 |
def clean_topic(topic):
|
12 |
topic = topic.lower()
|
@@ -18,46 +17,42 @@ def clean_topic(topic):
|
|
18 |
|
19 |
def generate_blog(topic):
|
20 |
topic = clean_topic(topic)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
try:
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
gr.Interface(
|
58 |
fn=generate_blog,
|
59 |
-
inputs=gr.Textbox(
|
60 |
outputs=gr.Textbox(label="Generated Blog"),
|
61 |
title="📝 AI Blog Writer",
|
62 |
-
description="
|
63 |
).launch(share=True)
|
|
|
1 |
import os
|
2 |
+
import requests
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Get token from environment variable (must be set in Hugging Face Secrets for Spaces)
|
6 |
+
HF_TOKEN = os.environ.get("OPENAI_API_KEY")
|
7 |
+
API_URL = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
|
8 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
|
9 |
|
10 |
def clean_topic(topic):
|
11 |
topic = topic.lower()
|
|
|
17 |
|
18 |
def generate_blog(topic):
|
19 |
topic = clean_topic(topic)
|
20 |
+
prompt = f"""### Instruction:
|
21 |
+
Write a complete, friendly, and engaging blog post about "{topic}".
|
22 |
+
Structure the blog with:
|
23 |
+
- A title
|
24 |
+
- An introduction
|
25 |
+
- 2–3 subheadings with paragraphs
|
26 |
+
- A conclusion
|
27 |
|
28 |
+
Use markdown formatting with ## for subheadings. Keep the tone conversational.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
### Response:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
"""
|
32 |
|
33 |
+
payload = {
|
34 |
+
"inputs": prompt,
|
35 |
+
"parameters": {
|
36 |
+
"max_new_tokens": 700,
|
37 |
+
"do_sample": True,
|
38 |
+
"temperature": 0.7,
|
39 |
+
"top_p": 0.9,
|
40 |
+
}
|
41 |
+
}
|
42 |
|
43 |
try:
|
44 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
45 |
+
response.raise_for_status()
|
46 |
+
generated_text = response.json()[0]['generated_text']
|
47 |
+
return generated_text.split("### Response:")[-1].strip()
|
48 |
+
except Exception as e:
|
49 |
+
return f"❌ Failed to generate blog: {str(e)}"
|
50 |
+
|
51 |
+
# Gradio Interface
|
52 |
gr.Interface(
|
53 |
fn=generate_blog,
|
54 |
+
inputs=gr.Textbox(label="Blog Topic"),
|
55 |
outputs=gr.Textbox(label="Generated Blog"),
|
56 |
title="📝 AI Blog Writer",
|
57 |
+
description="Enter a topic and get a well-written blog post.",
|
58 |
).launch(share=True)
|