Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,106 +2,59 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import requests
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# ----------------------------
|
| 8 |
-
# SECRETS
|
| 9 |
# ----------------------------
|
| 10 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 11 |
-
OCR_API_KEY = os.environ.get("OCR_API_KEY") # optional
|
| 12 |
|
| 13 |
if not GROQ_API_KEY:
|
| 14 |
raise ValueError("GROQ_API_KEY not found in environment variables.")
|
| 15 |
|
| 16 |
# ----------------------------
|
| 17 |
-
#
|
| 18 |
-
# ----------------------------
|
| 19 |
-
def extract_text_from_image(file_path):
|
| 20 |
-
if not OCR_API_KEY:
|
| 21 |
-
return ""
|
| 22 |
-
url = "https://api.ocr.space/parse/image"
|
| 23 |
-
with open(file_path, "rb") as f:
|
| 24 |
-
r = requests.post(
|
| 25 |
-
url,
|
| 26 |
-
files={"file": f},
|
| 27 |
-
data={"apikey": OCR_API_KEY, "language": "eng"}
|
| 28 |
-
)
|
| 29 |
-
try:
|
| 30 |
-
return r.json()['ParsedResults'][0]['ParsedText']
|
| 31 |
-
except:
|
| 32 |
-
return ""
|
| 33 |
-
|
| 34 |
-
# ----------------------------
|
| 35 |
-
# PDF FUNCTION
|
| 36 |
-
# ----------------------------
|
| 37 |
-
def extract_text_from_pdf(file_path):
|
| 38 |
-
text = ""
|
| 39 |
-
try:
|
| 40 |
-
doc = fitz.open(file_path)
|
| 41 |
-
for page in doc:
|
| 42 |
-
text += page.get_text()
|
| 43 |
-
except:
|
| 44 |
-
text = ""
|
| 45 |
-
return text
|
| 46 |
-
|
| 47 |
-
# ----------------------------
|
| 48 |
-
# PROMPT BUILDER
|
| 49 |
-
# ----------------------------
|
| 50 |
-
def build_prompt(user_text, file_text, link_info, mentions, hashtags, custom_prompt, checked_suggestions):
|
| 51 |
-
prompt = "You are a professional LinkedIn content creator.\n\n"
|
| 52 |
-
prompt += f"Main Content:\n{user_text}\n{file_text}\n\n"
|
| 53 |
-
|
| 54 |
-
if link_info:
|
| 55 |
-
prompt += f"Reference Link: {link_info.get('url')} ({link_info.get('description')})\n"
|
| 56 |
-
|
| 57 |
-
if mentions:
|
| 58 |
-
for m in mentions:
|
| 59 |
-
prompt += f"Mention {m.get('name')} ({m.get('role')}): {m.get('purpose')} | Link: {m.get('linkedin_url')}\n"
|
| 60 |
-
|
| 61 |
-
if hashtags:
|
| 62 |
-
prompt += f"Include hashtags: {', '.join(hashtags)}\n"
|
| 63 |
-
|
| 64 |
-
if custom_prompt:
|
| 65 |
-
prompt += f"User Instructions: {custom_prompt}\n"
|
| 66 |
-
|
| 67 |
-
if checked_suggestions:
|
| 68 |
-
prompt += f"Include suggestions: {', '.join(checked_suggestions)}\n"
|
| 69 |
-
|
| 70 |
-
prompt += "\nWrite a LinkedIn-ready post in human-like, engaging, professional style."
|
| 71 |
-
|
| 72 |
-
return prompt
|
| 73 |
-
|
| 74 |
-
# ----------------------------
|
| 75 |
-
# GENERATE POST FUNCTION (Groq GPT)
|
| 76 |
# ----------------------------
|
| 77 |
def generate_post(file, user_text, link_info, mentions, hashtags, custom_prompt, checked_suggestions):
|
| 78 |
file_text = ""
|
|
|
|
| 79 |
if file:
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
prompt = build_prompt(user_text, file_text, link_info, mentions, hashtags, custom_prompt, checked_suggestions)
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
headers = {
|
| 89 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 90 |
"Content-Type": "application/json"
|
| 91 |
}
|
| 92 |
data = {
|
| 93 |
-
"model": "groq-gpt-large", # example, replace with available model
|
| 94 |
"prompt": prompt,
|
| 95 |
"max_tokens": 400
|
| 96 |
}
|
| 97 |
-
|
| 98 |
-
r = requests.post(url, headers=headers, json=data)
|
| 99 |
try:
|
|
|
|
|
|
|
|
|
|
| 100 |
res_json = r.json()
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
elif "generated_text" in res_json:
|
| 104 |
return res_json["generated_text"]
|
|
|
|
|
|
|
| 105 |
else:
|
| 106 |
return f"Error: Unexpected response format: {res_json}"
|
| 107 |
except Exception as e:
|
|
@@ -112,7 +65,7 @@ def generate_post(file, user_text, link_info, mentions, hashtags, custom_prompt,
|
|
| 112 |
# ----------------------------
|
| 113 |
with gr.Blocks() as demo:
|
| 114 |
gr.Markdown("## LinkedIn Post Generator (Groq GPT + Gradio)")
|
| 115 |
-
|
| 116 |
file_input = gr.File(label="Upload PDF/Image", type="filepath")
|
| 117 |
user_text = gr.Textbox(label="Main Text", lines=3)
|
| 118 |
link_input = gr.Textbox(label="Reference Link URL")
|
|
@@ -130,9 +83,9 @@ with gr.Blocks() as demo:
|
|
| 130 |
"Make it professional"
|
| 131 |
])
|
| 132 |
mentions_input = gr.Textbox(label="Mentions JSON (name, role, purpose, linkedin_url)")
|
| 133 |
-
|
| 134 |
output_text = gr.Textbox(label="Generated LinkedIn Post", lines=10)
|
| 135 |
-
|
| 136 |
def wrap(file, user_text, link_input, link_desc, hashtags, custom_prompt, suggestions, mentions):
|
| 137 |
link_info = {"url": link_input, "description": link_desc} if link_input else None
|
| 138 |
hashtags_list = [h.strip() for h in hashtags.split(",")] if hashtags else []
|
|
@@ -141,7 +94,7 @@ with gr.Blocks() as demo:
|
|
| 141 |
except:
|
| 142 |
mentions_list = []
|
| 143 |
return generate_post(file, user_text, link_info, mentions_list, hashtags_list, custom_prompt, suggestions)
|
| 144 |
-
|
| 145 |
gr.Button("Generate Post").click(
|
| 146 |
wrap,
|
| 147 |
inputs=[file_input, user_text, link_input, link_desc, hashtags_input, custom_prompt_input, suggestions_input, mentions_input],
|
|
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import requests
|
| 5 |
+
from Pdf_utils import extract_text_from_pdf
|
| 6 |
+
from ocr_utils import extract_text_from_image
|
| 7 |
+
from PromptBuilder import build_prompt
|
| 8 |
|
| 9 |
# ----------------------------
|
| 10 |
+
# SECRETS
|
| 11 |
# ----------------------------
|
| 12 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 13 |
+
OCR_API_KEY = os.environ.get("OCR_API_KEY") # optional
|
| 14 |
|
| 15 |
if not GROQ_API_KEY:
|
| 16 |
raise ValueError("GROQ_API_KEY not found in environment variables.")
|
| 17 |
|
| 18 |
# ----------------------------
|
| 19 |
+
# GENERATE POST FUNCTION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# ----------------------------
|
| 21 |
def generate_post(file, user_text, link_info, mentions, hashtags, custom_prompt, checked_suggestions):
|
| 22 |
file_text = ""
|
| 23 |
+
# Extract text from uploaded file
|
| 24 |
if file:
|
| 25 |
+
try:
|
| 26 |
+
if file.name.lower().endswith(".pdf"):
|
| 27 |
+
file_text = extract_text_from_pdf(file.name)
|
| 28 |
+
else:
|
| 29 |
+
file_text = extract_text_from_image(file.name, OCR_API_KEY)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"File processing error: {str(e)}"
|
| 32 |
+
|
| 33 |
prompt = build_prompt(user_text, file_text, link_info, mentions, hashtags, custom_prompt, checked_suggestions)
|
| 34 |
+
|
| 35 |
+
# ----------------------------
|
| 36 |
+
# Groq GPT API call
|
| 37 |
+
# ----------------------------
|
| 38 |
+
url = "https://api.groq.com/v1/models/groq-gpt-large/infer" # replace with actual model-id
|
| 39 |
headers = {
|
| 40 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 41 |
"Content-Type": "application/json"
|
| 42 |
}
|
| 43 |
data = {
|
|
|
|
| 44 |
"prompt": prompt,
|
| 45 |
"max_tokens": 400
|
| 46 |
}
|
| 47 |
+
|
|
|
|
| 48 |
try:
|
| 49 |
+
r = requests.post(url, headers=headers, json=data, timeout=60)
|
| 50 |
+
if r.status_code != 200:
|
| 51 |
+
return f"API error {r.status_code}: {r.text}"
|
| 52 |
res_json = r.json()
|
| 53 |
+
# Safe parsing
|
| 54 |
+
if "generated_text" in res_json:
|
|
|
|
| 55 |
return res_json["generated_text"]
|
| 56 |
+
elif "choices" in res_json and len(res_json["choices"]) > 0:
|
| 57 |
+
return res_json["choices"][0].get("text", "")
|
| 58 |
else:
|
| 59 |
return f"Error: Unexpected response format: {res_json}"
|
| 60 |
except Exception as e:
|
|
|
|
| 65 |
# ----------------------------
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
gr.Markdown("## LinkedIn Post Generator (Groq GPT + Gradio)")
|
| 68 |
+
|
| 69 |
file_input = gr.File(label="Upload PDF/Image", type="filepath")
|
| 70 |
user_text = gr.Textbox(label="Main Text", lines=3)
|
| 71 |
link_input = gr.Textbox(label="Reference Link URL")
|
|
|
|
| 83 |
"Make it professional"
|
| 84 |
])
|
| 85 |
mentions_input = gr.Textbox(label="Mentions JSON (name, role, purpose, linkedin_url)")
|
| 86 |
+
|
| 87 |
output_text = gr.Textbox(label="Generated LinkedIn Post", lines=10)
|
| 88 |
+
|
| 89 |
def wrap(file, user_text, link_input, link_desc, hashtags, custom_prompt, suggestions, mentions):
|
| 90 |
link_info = {"url": link_input, "description": link_desc} if link_input else None
|
| 91 |
hashtags_list = [h.strip() for h in hashtags.split(",")] if hashtags else []
|
|
|
|
| 94 |
except:
|
| 95 |
mentions_list = []
|
| 96 |
return generate_post(file, user_text, link_info, mentions_list, hashtags_list, custom_prompt, suggestions)
|
| 97 |
+
|
| 98 |
gr.Button("Generate Post").click(
|
| 99 |
wrap,
|
| 100 |
inputs=[file_input, user_text, link_input, link_desc, hashtags_input, custom_prompt_input, suggestions_input, mentions_input],
|