jsakshi commited on
Commit
f2f7f9d
·
verified ·
1 Parent(s): fac7dd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -190
app.py CHANGED
@@ -341,7 +341,7 @@ if __name__ == "__main__":
341
  app.launch(share=True)'''
342
 
343
 
344
- '''import gradio as gr
345
  import os
346
  import time
347
  import requests
@@ -804,192 +804,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
804
  )
805
 
806
  if __name__ == "__main__":
807
- app.launch(share=True)'''
808
-
809
-
810
-
811
-
812
-
813
-
814
-
815
-
816
-
817
-
818
-
819
-
820
-
821
-
822
- import gradio as gr
823
- import os
824
- import time
825
- import requests
826
- import re
827
- import uuid
828
- import markdown
829
- from datetime import datetime
830
- from dotenv import load_dotenv
831
- from huggingface_hub import HfApi, upload_file
832
-
833
- load_dotenv()
834
-
835
- # Configuration
836
- HF_TOKEN = os.getenv("HF_TOKEN")
837
- HF_USERNAME = "jsakshi"
838
- AUTHOR_NAME = "Sarah Johnson"
839
- HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
840
-
841
- def generate_blog_content(topic, seo_keywords):
842
- try:
843
- prompt = f"""Write a high-quality blog post about {topic} that sounds human-written. Include:
844
- - Engaging introduction with a hook
845
- - 3-5 main sections with subheadings
846
- - Bold key terms using **bold**
847
- - SEO keywords: {', '.join(seo_keywords)}
848
- - Conclusion with actionable advice
849
- - Author attribution for {AUTHOR_NAME}
850
- Avoid AI-sounding language. Use conversational tone."""
851
-
852
- response = requests.post(
853
- "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2",
854
- headers=HEADERS,
855
- json={"inputs": prompt, "parameters": {"max_length": 2500}}
856
- )
857
-
858
- if response.status_code != 200:
859
- return None, f"API Error: {response.text}"
860
-
861
- blog_text = response.json()[0]['generated_text']
862
- blog_text += f"\n\n*This article was written by {AUTHOR_NAME}, combining research and practical experience.*"
863
-
864
- return {
865
- "title": extract_title(blog_text),
866
- "content": blog_text,
867
- "bold_terms": re.findall(r'\*\*(.*?)\*\*', blog_text)
868
- }, None
869
-
870
- except Exception as e:
871
- return None, f"Error: {str(e)}"
872
-
873
- def extract_title(text):
874
- lines = text.split('\n')
875
- for line in lines:
876
- if line.strip() and not line.startswith('#'):
877
- return line.strip().replace('#', '')
878
- return "Professional Analysis"
879
-
880
- def create_hosted_blog(topic, seo_keywords):
881
- try:
882
- # Generate content
883
- content_data, error = generate_blog_content(topic, seo_keywords)
884
- if error:
885
- return f"Error: {error}", ""
886
-
887
- # Create space
888
- space_id = f"blog-{uuid.uuid4().hex[:8]}"
889
- space_name = f"{HF_USERNAME}/{space_id}"
890
- api = HfApi(token=HF_TOKEN)
891
- api.create_repo(
892
- repo_id=space_name,
893
- repo_type="space",
894
- space_sdk="static",
895
- private=False
896
- )
897
-
898
- # Create HTML with improved styling
899
- html_content = f"""
900
- <!DOCTYPE html>
901
- <html>
902
- <head>
903
- <title>{content_data['title']}</title>
904
- <style>
905
- body {{
906
- font-family: Arial, sans-serif;
907
- line-height: 1.6;
908
- background-color: #f4f4f4;
909
- color: #333;
910
- margin: 0;
911
- padding: 0;
912
- }}
913
- .container {{
914
- width: 80%;
915
- margin: auto;
916
- overflow: hidden;
917
- background: white;
918
- padding: 20px;
919
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
920
- border-radius: 10px;
921
- }}
922
- h1, h2, h3 {{
923
- color: #222;
924
- }}
925
- header {{
926
- background: linear-gradient(135deg, #ff7e5f, #feb47b);
927
- color: white;
928
- padding: 20px 0;
929
- text-align: center;
930
- border-radius: 10px 10px 0 0;
931
- }}
932
- footer {{
933
- text-align: center;
934
- padding: 10px;
935
- background: #222;
936
- color: white;
937
- margin-top: 20px;
938
- border-radius: 0 0 10px 10px;
939
- }}
940
- img {{
941
- width: 100%;
942
- border-radius: 10px;
943
- margin-bottom: 10px;
944
- }}
945
- </style>
946
- </head>
947
- <body>
948
- <header>
949
- <h1>{content_data['title']}</h1>
950
- </header>
951
- <div class="container">
952
- {markdown.markdown(content_data['content'])}
953
- </div>
954
- <footer>
955
- <p>© 2025 {AUTHOR_NAME}. All Rights Reserved.</p>
956
- </footer>
957
- </body>
958
- </html>
959
- """
960
-
961
- # Upload HTML
962
- upload_file(
963
- path_or_fileobj=html_content.encode(),
964
- path_in_repo="index.html",
965
- repo_id=space_name,
966
- repo_type="space"
967
- )
968
-
969
- return f"https://huggingface.co/spaces/{space_name}", content_data['content']
970
-
971
- except Exception as e:
972
- return f"Error: {str(e)}", ""
973
-
974
- # Gradio interface
975
- with gr.Blocks() as app:
976
- gr.Markdown("# Professional Blog Generator")
977
-
978
- with gr.Row():
979
- with gr.Column():
980
- topic_input = gr.Textbox(label="Topic")
981
- seo_input = gr.Textbox(label="SEO Keywords (comma-separated)")
982
- generate_btn = gr.Button("Generate", variant="primary")
983
-
984
- with gr.Column():
985
- blog_link = gr.Markdown()
986
- blog_output = gr.Markdown()
987
-
988
- generate_btn.click(
989
- fn=create_hosted_blog,
990
- inputs=[topic_input, seo_input],
991
- outputs=[blog_link, blog_output]
992
- )
993
-
994
- if __name__ == "__main__":
995
- app.launch(share=True)
 
341
  app.launch(share=True)'''
342
 
343
 
344
+ import gradio as gr
345
  import os
346
  import time
347
  import requests
 
804
  )
805
 
806
  if __name__ == "__main__":
807
+ app.launch(share=True)