Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import requests
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# ==============================
|
| 7 |
+
# GROQ API CONFIG
|
| 8 |
+
# ==============================
|
| 9 |
+
|
| 10 |
+
GROQ_API_KEY = os.environ.get("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")
|
| 11 |
+
GROQ_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 12 |
+
|
| 13 |
+
MODEL = "llama3-70b-8192" # fast + powerful
|
| 14 |
+
|
| 15 |
+
# ==============================
|
| 16 |
+
# PROPOSAL GENERATION FUNCTION
|
| 17 |
+
# ==============================
|
| 18 |
+
|
| 19 |
+
def generate_proposal(job_description, skills, experience_level, tone):
|
| 20 |
+
|
| 21 |
+
if not GROQ_API_KEY:
|
| 22 |
+
return "Error: GROQ_API_KEY not found in environment variables."
|
| 23 |
+
|
| 24 |
+
prompt = f"""
|
| 25 |
+
You are a top 1% freelancer who writes high-converting proposals.
|
| 26 |
+
|
| 27 |
+
Your task:
|
| 28 |
+
Generate a professional, persuasive, personalized freelance proposal.
|
| 29 |
+
|
| 30 |
+
Job Description:
|
| 31 |
+
{job_description}
|
| 32 |
+
|
| 33 |
+
Freelancer Skills:
|
| 34 |
+
{skills}
|
| 35 |
+
|
| 36 |
+
Experience Level:
|
| 37 |
+
{experience_level}
|
| 38 |
+
|
| 39 |
+
Tone:
|
| 40 |
+
{tone}
|
| 41 |
+
|
| 42 |
+
Return ONLY valid JSON in this format:
|
| 43 |
+
|
| 44 |
+
{{
|
| 45 |
+
"hook": "",
|
| 46 |
+
"proposal_body": "",
|
| 47 |
+
"pricing_suggestion": "",
|
| 48 |
+
"follow_up_message": ""
|
| 49 |
+
}}
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
headers = {
|
| 53 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 54 |
+
"Content-Type": "application/json"
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
payload = {
|
| 58 |
+
"model": MODEL,
|
| 59 |
+
"messages": [
|
| 60 |
+
{"role": "system", "content": "You write winning freelance proposals."},
|
| 61 |
+
{"role": "user", "content": prompt}
|
| 62 |
+
],
|
| 63 |
+
"temperature": 0.7
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
response = requests.post(GROQ_URL, headers=headers, json=payload)
|
| 68 |
+
result = response.json()
|
| 69 |
+
|
| 70 |
+
content = result["choices"][0]["message"]["content"]
|
| 71 |
+
|
| 72 |
+
# Clean JSON if wrapped in code block
|
| 73 |
+
if content.startswith("```"):
|
| 74 |
+
content = content.split("```")[1]
|
| 75 |
+
|
| 76 |
+
data = json.loads(content)
|
| 77 |
+
|
| 78 |
+
final_output = f"""
|
| 79 |
+
🔥 HOOK:
|
| 80 |
+
{data['hook']}
|
| 81 |
+
|
| 82 |
+
📄 PROPOSAL:
|
| 83 |
+
{data['proposal_body']}
|
| 84 |
+
|
| 85 |
+
💰 PRICING SUGGESTION:
|
| 86 |
+
{data['pricing_suggestion']}
|
| 87 |
+
|
| 88 |
+
📩 FOLLOW-UP MESSAGE:
|
| 89 |
+
{data['follow_up_message']}
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
return final_output
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"Error generating proposal: {str(e)}"
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# ==============================
|
| 99 |
+
# GRADIO UI
|
| 100 |
+
# ==============================
|
| 101 |
+
|
| 102 |
+
with gr.Blocks(title="AI Proposal Generator for Freelancers") as app:
|
| 103 |
+
|
| 104 |
+
gr.Markdown("## 🚀 AI Proposal Generator")
|
| 105 |
+
gr.Markdown("Generate high-converting freelance proposals instantly.")
|
| 106 |
+
|
| 107 |
+
job_description = gr.Textbox(label="Job Description", lines=8)
|
| 108 |
+
skills = gr.Textbox(label="Your Skills", lines=5)
|
| 109 |
+
|
| 110 |
+
experience_level = gr.Dropdown(
|
| 111 |
+
["Beginner", "Intermediate", "Expert"],
|
| 112 |
+
label="Experience Level",
|
| 113 |
+
value="Intermediate"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
tone = gr.Dropdown(
|
| 117 |
+
["Professional", "Confident", "Friendly", "Persuasive"],
|
| 118 |
+
label="Tone",
|
| 119 |
+
value="Professional"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
generate_btn = gr.Button("Generate Proposal")
|
| 123 |
+
|
| 124 |
+
output = gr.Textbox(label="Generated Proposal", lines=20)
|
| 125 |
+
|
| 126 |
+
generate_btn.click(
|
| 127 |
+
generate_proposal,
|
| 128 |
+
inputs=[job_description, skills, experience_level, tone],
|
| 129 |
+
outputs=output
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
app.launch()
|