Linkedin connect
Browse files
app.py
CHANGED
|
@@ -1,64 +1,96 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from urllib.parse import urlencode, parse_qs
|
| 5 |
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
| 6 |
+
import threading
|
| 7 |
+
|
| 8 |
+
# Your LinkedIn App credentials
|
| 9 |
+
CLIENT_ID = os.getenv("LINKEDIN_CLIENT_ID")
|
| 10 |
+
CLIENT_SECRET = os.getenv("LINKEDIN_CLIENT_SECRET")
|
| 11 |
+
REDIRECT_URI = "http://localhost:8080/callback"
|
| 12 |
+
AUTH_URL = "https://www.linkedin.com/oauth/v2/authorization"
|
| 13 |
+
TOKEN_URL = "https://www.linkedin.com/oauth/v2/accessToken"
|
| 14 |
+
PROFILE_API = "https://api.linkedin.com/v2/me"
|
| 15 |
+
EMAIL_API = "https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))"
|
| 16 |
+
|
| 17 |
+
access_token = None
|
| 18 |
+
|
| 19 |
+
# Local web server to handle redirect URI callback
|
| 20 |
+
class OAuthCallbackHandler(BaseHTTPRequestHandler):
|
| 21 |
+
def do_GET(self):
|
| 22 |
+
global access_token
|
| 23 |
+
query = parse_qs(self.path.split('?', 1)[-1])
|
| 24 |
+
code = query.get('code')[0]
|
| 25 |
+
|
| 26 |
+
# Exchange code for token
|
| 27 |
+
token_data = {
|
| 28 |
+
'grant_type': 'authorization_code',
|
| 29 |
+
'code': code,
|
| 30 |
+
'redirect_uri': REDIRECT_URI,
|
| 31 |
+
'client_id': CLIENT_ID,
|
| 32 |
+
'client_secret': CLIENT_SECRET
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
response = requests.post(TOKEN_URL, data=token_data)
|
| 36 |
+
token_json = response.json()
|
| 37 |
+
access_token = token_json.get('access_token')
|
| 38 |
+
|
| 39 |
+
self.send_response(200)
|
| 40 |
+
self.end_headers()
|
| 41 |
+
self.wfile.write(b'Authentication successful. Return to the app.')
|
| 42 |
+
|
| 43 |
+
# Run server in background
|
| 44 |
+
def start_oauth_server():
|
| 45 |
+
server = HTTPServer(('localhost', 8080), OAuthCallbackHandler)
|
| 46 |
+
server.handle_request()
|
| 47 |
+
|
| 48 |
+
def get_authorization_url():
|
| 49 |
+
params = {
|
| 50 |
+
'response_type': 'code',
|
| 51 |
+
'client_id': CLIENT_ID,
|
| 52 |
+
'redirect_uri': REDIRECT_URI,
|
| 53 |
+
'scope': 'r_liteprofile r_emailaddress'
|
| 54 |
+
}
|
| 55 |
+
return f"{AUTH_URL}?{urlencode(params)}"
|
| 56 |
+
|
| 57 |
+
# Hugging Face summarization
|
| 58 |
+
def summarize_profile(name, headline, email):
|
| 59 |
+
text = f"{name} is a professional with expertise in: {headline}. Contact: {email}"
|
| 60 |
+
response = requests.post(
|
| 61 |
+
"https://api-inference.huggingface.co/models/facebook/bart-large-cnn",
|
| 62 |
+
headers={"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"},
|
| 63 |
+
json={"inputs": text}
|
| 64 |
+
)
|
| 65 |
+
return response.json()[0]["summary_text"]
|
| 66 |
+
|
| 67 |
+
def linkedIn_summary_flow():
|
| 68 |
+
global access_token
|
| 69 |
+
# Trigger OAuth
|
| 70 |
+
threading.Thread(target=start_oauth_server).start()
|
| 71 |
+
return f"[Click here to authorize LinkedIn]({get_authorization_url()})"
|
| 72 |
+
|
| 73 |
+
def fetch_and_summarize(_):
|
| 74 |
+
headers = {"Authorization": f"Bearer {access_token}"}
|
| 75 |
+
profile = requests.get(PROFILE_API, headers=headers).json()
|
| 76 |
+
email_data = requests.get(EMAIL_API, headers=headers).json()
|
| 77 |
+
|
| 78 |
+
name = profile.get("localizedFirstName", "") + " " + profile.get("localizedLastName", "")
|
| 79 |
+
headline = profile.get("localizedHeadline", "")
|
| 80 |
+
email = email_data['elements'][0]['handle~']['emailAddress']
|
| 81 |
+
|
| 82 |
+
summary = summarize_profile(name, headline, email)
|
| 83 |
+
return f"**Name:** {name}\n**Headline:** {headline}\n**Email:** {email}\n\n**Summary:** {summary}"
|
| 84 |
+
|
| 85 |
+
with gr.Blocks() as demo:
|
| 86 |
+
gr.Markdown("# π LinkedIn Profile Summarizer")
|
| 87 |
+
gr.Markdown("1. Click to authorize via LinkedIn\n2. Fetch & summarize your profile")
|
| 88 |
+
|
| 89 |
+
btn_auth = gr.Button("π Connect to LinkedIn")
|
| 90 |
+
btn_fetch = gr.Button("π Summarize My Profile")
|
| 91 |
+
output = gr.Textbox(label="Summary", lines=10)
|
| 92 |
+
|
| 93 |
+
btn_auth.click(linkedIn_summary_flow, outputs=output)
|
| 94 |
+
btn_fetch.click(fetch_and_summarize, inputs=[], outputs=output)
|
| 95 |
+
|
| 96 |
+
demo.launch()
|