jsakshi commited on
Commit
8255e46
·
verified ·
1 Parent(s): 5b78945

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -22
app.py CHANGED
@@ -178,7 +178,7 @@ load_dotenv()
178
 
179
  # Configuration
180
  HF_TOKEN = os.getenv("HF_TOKEN")
181
- HF_USERNAME = "jsakshi" # Replace with your username
182
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
183
 
184
  def generate_blog_content(topic):
@@ -196,12 +196,22 @@ def generate_blog_content(topic):
196
  headers=HEADERS,
197
  json={"inputs": prompt, "parameters": {"max_length": 2000}}
198
  )
199
- return response.json()[0]['generated_text']
 
 
 
 
 
200
  except Exception as e:
201
- return f"Error: {str(e)}"
202
 
203
- def create_hosted_blog(topic, content):
204
  try:
 
 
 
 
 
205
  # Create unique space
206
  space_id = f"blog-{uuid.uuid4().hex[:8]}"
207
  space_name = f"{HF_USERNAME}/{space_id}"
@@ -223,19 +233,23 @@ def create_hosted_blog(topic, content):
223
  headers=HEADERS,
224
  json={"inputs": f"Professional illustration about {topic}, clean design"}
225
  )
226
- upload_file(
227
- path_or_fileobj=response.content,
228
- path_in_repo=f"image_{idx}.png",
229
- repo_id=space_name,
230
- repo_type="space"
231
- )
232
- image_paths.append(f"image_{idx}.png")
 
233
  time.sleep(1)
234
 
235
- # Replace image placeholders first
236
- updated_content = content.replace("[IMAGE1]", f'![Image 1]({image_paths[0]})').replace("[IMAGE2]", f'![Image 2]({image_paths[1]})')
237
-
238
- # Create professional HTML template
 
 
 
239
  html_content = f"""
240
  <!DOCTYPE html>
241
  <html>
@@ -261,10 +275,6 @@ def create_hosted_blog(topic, content):
261
  font-size: 2.5em;
262
  margin-bottom: 10px;
263
  }}
264
- h2 {{
265
- color: #34495e;
266
- margin-top: 40px;
267
- }}
268
  img {{
269
  width: 100%;
270
  height: auto;
@@ -304,13 +314,14 @@ def create_hosted_blog(topic, content):
304
  except Exception as e:
305
  return f"Error: {str(e)}", ""
306
 
307
- # Clean Gradio interface
308
  with gr.Blocks(theme=gr.themes.Soft()) as app:
309
  gr.Markdown("# 📄 Professional Blog Generator")
310
 
311
  with gr.Row():
312
  with gr.Column():
313
- topic_input = gr.Textbox(label="Enter Blog Topic", placeholder="e.g., Future of Renewable Energy")
 
314
  generate_btn = gr.Button("Generate Blog", variant="primary")
315
 
316
  with gr.Column():
@@ -327,5 +338,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
327
  )
328
 
329
  if __name__ == "__main__":
330
- app.launch(share=True)
331
  app.launch(share=True)
 
178
 
179
  # Configuration
180
  HF_TOKEN = os.getenv("HF_TOKEN")
181
+ HF_USERNAME = "jsakshi"
182
  HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
183
 
184
  def generate_blog_content(topic):
 
196
  headers=HEADERS,
197
  json={"inputs": prompt, "parameters": {"max_length": 2000}}
198
  )
199
+
200
+ if response.status_code != 200:
201
+ return None, f"API Error: {response.text}"
202
+
203
+ return response.json()[0]['generated_text'], None
204
+
205
  except Exception as e:
206
+ return None, f"Error: {str(e)}"
207
 
208
+ def create_hosted_blog(topic):
209
  try:
210
+ # Generate blog content first
211
+ content, error = generate_blog_content(topic)
212
+ if error or not content:
213
+ return f"Error generating content: {error}", ""
214
+
215
  # Create unique space
216
  space_id = f"blog-{uuid.uuid4().hex[:8]}"
217
  space_name = f"{HF_USERNAME}/{space_id}"
 
233
  headers=HEADERS,
234
  json={"inputs": f"Professional illustration about {topic}, clean design"}
235
  )
236
+ if response.status_code == 200:
237
+ upload_file(
238
+ path_or_fileobj=response.content,
239
+ path_in_repo=f"image_{idx}.png",
240
+ repo_id=space_name,
241
+ repo_type="space"
242
+ )
243
+ image_paths.append(f"image_{idx}.png")
244
  time.sleep(1)
245
 
246
+ # Replace image placeholders
247
+ if content:
248
+ updated_content = content.replace("[IMAGE1]", f'![Image 1]({image_paths[0]})').replace("[IMAGE2]", f'![Image 2]({image_paths[1]})')
249
+ else:
250
+ return "Error: No content generated", ""
251
+
252
+ # Create HTML template
253
  html_content = f"""
254
  <!DOCTYPE html>
255
  <html>
 
275
  font-size: 2.5em;
276
  margin-bottom: 10px;
277
  }}
 
 
 
 
278
  img {{
279
  width: 100%;
280
  height: auto;
 
314
  except Exception as e:
315
  return f"Error: {str(e)}", ""
316
 
317
+ # Gradio interface
318
  with gr.Blocks(theme=gr.themes.Soft()) as app:
319
  gr.Markdown("# 📄 Professional Blog Generator")
320
 
321
  with gr.Row():
322
  with gr.Column():
323
+ topic_input = gr.Textbox(label="Enter Blog Topic",
324
+ placeholder="e.g., Future of AI in Healthcare")
325
  generate_btn = gr.Button("Generate Blog", variant="primary")
326
 
327
  with gr.Column():
 
338
  )
339
 
340
  if __name__ == "__main__":
 
341
  app.launch(share=True)