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OpenAI
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
import base64
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
from urllib.parse import urlparse
|
| 8 |
+
|
| 9 |
+
# Load environment variables from .env file
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
# Initialize Azure OpenAI client
|
| 13 |
+
def get_openai_client():
|
| 14 |
+
try:
|
| 15 |
+
return AzureOpenAI(
|
| 16 |
+
api_key=os.getenv('AZURE_OPENAI_API_KEY').strip(),
|
| 17 |
+
api_version=os.getenv('AZURE_OPENAI_API_VERSION').strip(),
|
| 18 |
+
azure_endpoint=os.getenv('AZURE_OPENAI_ENDPOINT').strip(),
|
| 19 |
+
)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Failed to initialize OpenAI client: {e}")
|
| 22 |
+
return None
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Expanded list of important files that usually hold project logic
|
| 26 |
+
important_files = [
|
| 27 |
+
# Documentation
|
| 28 |
+
"README.md", "README.rst",
|
| 29 |
+
|
| 30 |
+
# Python backends
|
| 31 |
+
"app.py", "main.py", "server.py", "bot.py", "manage.py",
|
| 32 |
+
"config.py", "settings.py", "routes.py",
|
| 33 |
+
|
| 34 |
+
# Node.js / Express
|
| 35 |
+
"index.js", "server.js", "app.js",
|
| 36 |
+
|
| 37 |
+
# React / Next.js
|
| 38 |
+
"App.tsx", "page.tsx", "index.tsx", "_app.tsx", "_document.tsx",
|
| 39 |
+
|
| 40 |
+
# Angular / Vue
|
| 41 |
+
"main.ts", "app.component.ts", "App.vue",
|
| 42 |
+
|
| 43 |
+
# ML/AI Core
|
| 44 |
+
"model.py", "train.py", "predict.py", "inference.py",
|
| 45 |
+
|
| 46 |
+
# Setup / Environment
|
| 47 |
+
"requirements.txt", "package.json", "pyproject.toml",
|
| 48 |
+
"setup.py", "Dockerfile", ".env.example"
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def fetch_repo_files(repo_url):
|
| 53 |
+
"""
|
| 54 |
+
Fetch important files from a public GitHub repo.
|
| 55 |
+
"""
|
| 56 |
+
try:
|
| 57 |
+
parsed = urlparse(repo_url)
|
| 58 |
+
path_parts = parsed.path.strip("/").split("/")
|
| 59 |
+
if len(path_parts) < 2:
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
owner, repo = path_parts[0], path_parts[1].replace(".git", "")
|
| 63 |
+
api_url = f"https://api.github.com/repos/{owner}/{repo}/contents"
|
| 64 |
+
|
| 65 |
+
repo_content = {}
|
| 66 |
+
for file in important_files:
|
| 67 |
+
file_url = f"{api_url}/{file}"
|
| 68 |
+
resp = requests.get(file_url)
|
| 69 |
+
if resp.status_code == 200:
|
| 70 |
+
data = resp.json()
|
| 71 |
+
if data.get("encoding") == "base64":
|
| 72 |
+
content = base64.b64decode(data["content"]).decode("utf-8", errors="ignore")
|
| 73 |
+
else:
|
| 74 |
+
content = data.get("content", "")
|
| 75 |
+
repo_content[file] = content
|
| 76 |
+
|
| 77 |
+
return repo_content if repo_content else None
|
| 78 |
+
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"[ERROR] Failed to fetch repo files: {e}")
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def generate_post(topic, tone="Professional"):
|
| 85 |
+
try:
|
| 86 |
+
client = get_openai_client()
|
| 87 |
+
if not client:
|
| 88 |
+
return "❌ OpenAI client initialization failed. Check API settings."
|
| 89 |
+
|
| 90 |
+
if not topic:
|
| 91 |
+
return "⚠️ Please provide a topic or GitHub repo link."
|
| 92 |
+
|
| 93 |
+
# Check if topic is a GitHub repo link
|
| 94 |
+
repo_content = None
|
| 95 |
+
if topic.startswith("http") and "github.com" in topic:
|
| 96 |
+
repo_content = fetch_repo_files(topic)
|
| 97 |
+
|
| 98 |
+
if repo_content:
|
| 99 |
+
combined_summary = "\n\n".join(
|
| 100 |
+
[f"--- {fname} ---\n{content}" for fname, content in repo_content.items()]
|
| 101 |
+
)
|
| 102 |
+
prompt = (
|
| 103 |
+
f"Write a {tone.lower()} LinkedIn post summarizing the following GitHub project.\n\n"
|
| 104 |
+
f"Focus on the purpose, tech stack, and innovation:\n\n{combined_summary}"
|
| 105 |
+
)
|
| 106 |
+
else:
|
| 107 |
+
prompt = f"Write a {tone.lower()} LinkedIn post about: {topic}"
|
| 108 |
+
|
| 109 |
+
messages = [
|
| 110 |
+
{"role": "system", "content": "You are a professional LinkedIn post writer."},
|
| 111 |
+
{"role": "user", "content": prompt}
|
| 112 |
+
]
|
| 113 |
+
|
| 114 |
+
response = client.chat.completions.create(
|
| 115 |
+
model=os.getenv('AZURE_OPENAI_DEPLOYMENT'),
|
| 116 |
+
messages=messages,
|
| 117 |
+
max_tokens=1000,
|
| 118 |
+
temperature=0.9,
|
| 119 |
+
top_p=1,
|
| 120 |
+
frequency_penalty=0,
|
| 121 |
+
presence_penalty=0
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
return response.choices[0].message.content
|
| 125 |
+
|
| 126 |
+
except Exception as e:
|
| 127 |
+
return f"❌ Error: {str(e)}"
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# Build Gradio interface
|
| 131 |
+
with gr.Blocks() as demo:
|
| 132 |
+
gr.Markdown("# 🚀 LinkedIn Post Generator\nGenerate professional LinkedIn posts powered by Azure OpenAI.\n\nPaste a **topic** or a **GitHub repo link**.")
|
| 133 |
+
|
| 134 |
+
with gr.Row():
|
| 135 |
+
topic_input = gr.Textbox(label="Topic or GitHub Repo Link", placeholder="Enter project topic or GitHub repo link...")
|
| 136 |
+
|
| 137 |
+
tone_input = gr.Dropdown(
|
| 138 |
+
["Professional", "Casual", "Excited", "Thoughtful", "Inspirational"],
|
| 139 |
+
value="Professional",
|
| 140 |
+
label="Tone"
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
output = gr.Textbox(label="Generated LinkedIn Post", lines=15)
|
| 144 |
+
|
| 145 |
+
generate_btn = gr.Button("✨ Generate Post")
|
| 146 |
+
generate_btn.click(fn=generate_post, inputs=[topic_input, tone_input], outputs=output)
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# Run app
|
| 150 |
+
if __name__ == "__main__":
|
| 151 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False, pwa=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
flask-cors
|
| 3 |
+
openai
|
| 4 |
+
python-dotenv
|
| 5 |
+
urllib3
|
| 6 |
+
gradio
|