Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,16 +7,17 @@ import fitz # PyMuPDF for PDFs
|
|
| 7 |
# ----------------------------
|
| 8 |
# SECRETS FROM ENVIRONMENT
|
| 9 |
# ----------------------------
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
if not HF_API_KEY or not OCR_API_KEY:
|
| 14 |
-
raise ValueError("HF_API_KEY or OCR_API_KEY not found in environment variables.")
|
| 15 |
|
| 16 |
# ----------------------------
|
| 17 |
# OCR FUNCTION
|
| 18 |
# ----------------------------
|
| 19 |
def extract_text_from_image(file_path):
|
|
|
|
|
|
|
|
|
|
| 20 |
url = "https://api.ocr.space/parse/image"
|
| 21 |
with open(file_path, "rb") as f:
|
| 22 |
r = requests.post(
|
|
@@ -36,9 +37,12 @@ def extract_text_from_image(file_path):
|
|
| 36 |
# ----------------------------
|
| 37 |
def extract_text_from_pdf(file_path):
|
| 38 |
text = ""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
return text
|
| 43 |
|
| 44 |
# ----------------------------
|
|
@@ -75,10 +79,8 @@ def build_prompt(user_text, file_text, link_info, mentions, hashtags, custom_pro
|
|
| 75 |
return prompt
|
| 76 |
|
| 77 |
# ----------------------------
|
| 78 |
-
# GENERATE POST FUNCTION
|
| 79 |
# ----------------------------
|
| 80 |
-
HF_MODEL = "Qwen2.5-Coder-7B-Instruct" # Hugging Face model
|
| 81 |
-
|
| 82 |
def generate_post(file, user_text, link_info, mentions, hashtags, custom_prompt, checked_suggestions):
|
| 83 |
file_text = ""
|
| 84 |
if file:
|
|
@@ -89,14 +91,19 @@ def generate_post(file, user_text, link_info, mentions, hashtags, custom_prompt,
|
|
| 89 |
|
| 90 |
prompt = build_prompt(user_text, file_text, link_info, mentions, hashtags, custom_prompt, checked_suggestions)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
try:
|
| 98 |
-
|
| 99 |
-
return result[0]["generated_text"]
|
| 100 |
except Exception as e:
|
| 101 |
return f"Error generating post: {str(e)}"
|
| 102 |
|
|
@@ -104,7 +111,7 @@ def generate_post(file, user_text, link_info, mentions, hashtags, custom_prompt,
|
|
| 104 |
# GRADIO UI
|
| 105 |
# ----------------------------
|
| 106 |
with gr.Blocks() as demo:
|
| 107 |
-
gr.Markdown("## LinkedIn Post Generator (
|
| 108 |
|
| 109 |
file_input = gr.File(label="Upload PDF/Image", type="filepath")
|
| 110 |
user_text = gr.Textbox(label="Main Text", lines=3)
|
|
|
|
| 7 |
# ----------------------------
|
| 8 |
# SECRETS FROM ENVIRONMENT
|
| 9 |
# ----------------------------
|
| 10 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 11 |
+
if not GROQ_API_KEY:
|
| 12 |
+
raise ValueError("GROQ_API_KEY not found in environment variables.")
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# ----------------------------
|
| 15 |
# OCR FUNCTION
|
| 16 |
# ----------------------------
|
| 17 |
def extract_text_from_image(file_path):
|
| 18 |
+
OCR_API_KEY = os.environ.get("OCR_API_KEY")
|
| 19 |
+
if not OCR_API_KEY:
|
| 20 |
+
return ""
|
| 21 |
url = "https://api.ocr.space/parse/image"
|
| 22 |
with open(file_path, "rb") as f:
|
| 23 |
r = requests.post(
|
|
|
|
| 37 |
# ----------------------------
|
| 38 |
def extract_text_from_pdf(file_path):
|
| 39 |
text = ""
|
| 40 |
+
try:
|
| 41 |
+
doc = fitz.open(file_path)
|
| 42 |
+
for page in doc:
|
| 43 |
+
text += page.get_text()
|
| 44 |
+
except:
|
| 45 |
+
text = ""
|
| 46 |
return text
|
| 47 |
|
| 48 |
# ----------------------------
|
|
|
|
| 79 |
return prompt
|
| 80 |
|
| 81 |
# ----------------------------
|
| 82 |
+
# GENERATE POST FUNCTION (Groq GPT)
|
| 83 |
# ----------------------------
|
|
|
|
|
|
|
| 84 |
def generate_post(file, user_text, link_info, mentions, hashtags, custom_prompt, checked_suggestions):
|
| 85 |
file_text = ""
|
| 86 |
if file:
|
|
|
|
| 91 |
|
| 92 |
prompt = build_prompt(user_text, file_text, link_info, mentions, hashtags, custom_prompt, checked_suggestions)
|
| 93 |
|
| 94 |
+
url = "https://api.groq.com/v1/generate" # Replace with correct Groq GPT endpoint if needed
|
| 95 |
+
headers = {
|
| 96 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 97 |
+
"Content-Type": "application/json"
|
| 98 |
+
}
|
| 99 |
+
data = {
|
| 100 |
+
"model": "groq-gpt-large", # Example model, change to available Groq GPT model
|
| 101 |
+
"prompt": prompt,
|
| 102 |
+
"max_tokens": 400
|
| 103 |
+
}
|
| 104 |
+
r = requests.post(url, headers=headers, json=data)
|
| 105 |
try:
|
| 106 |
+
return r.json()["text"]
|
|
|
|
| 107 |
except Exception as e:
|
| 108 |
return f"Error generating post: {str(e)}"
|
| 109 |
|
|
|
|
| 111 |
# GRADIO UI
|
| 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)
|