Pooja P commited on
Commit
bfe3b07
·
1 Parent(s): f96f50a

added new app1.py with modification

Browse files
Files changed (1) hide show
  1. app1.py +33 -38
app1.py CHANGED
@@ -1,12 +1,11 @@
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()
@@ -18,46 +17,42 @@ def clean_topic(topic):
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)
 
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)