File size: 3,577 Bytes
d532efd
b7615ee
 
 
 
 
 
 
3c0e98f
 
 
b7615ee
 
 
 
 
4582f15
b7615ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf1fdc6
b7615ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf1fdc6
b7615ee
 
 
 
 
 
 
 
 
 
 
bf1fdc6
b7615ee
 
 
 
 
bf1fdc6
b7615ee
bf1fdc6
b7615ee
 
bf1fdc6
b7615ee
 
 
d532efd
b7615ee
 
 
 
 
 
bf1fdc6
b7615ee
 
 
bf1fdc6
b7615ee
 
bf1fdc6
b7615ee
1
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import gradio as gr
import requests
import os
from urllib.parse import urlencode, parse_qs
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading

# Your LinkedIn App credentials
CLIENT_ID = "866hp4xfy5zmxz" #os.getenv("LINKEDIN_CLIENT_ID")
CLIENT_SECRET = "WPL_AP1.p8sN30biu6BLlTn7.sudQlw==" #os.getenv("LINKEDIN_CLIENT_SECRET")
REDIRECT_URI = "https://tkgauri.hf.space/callback"
AUTH_URL = "https://www.linkedin.com/oauth/v2/authorization"
TOKEN_URL = "https://www.linkedin.com/oauth/v2/accessToken"
PROFILE_API = "https://api.linkedin.com/v2/me"
EMAIL_API = "https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))"

access_token = "new_token"

# Local web server to handle redirect URI callback
class OAuthCallbackHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        global access_token
        query = parse_qs(self.path.split('?', 1)[-1])
        code = query.get('code')[0]

        # Exchange code for token
        token_data = {
            'grant_type': 'authorization_code',
            'code': code,
            'redirect_uri': REDIRECT_URI,
            'client_id': CLIENT_ID,
            'client_secret': CLIENT_SECRET
        }

        response = requests.post(TOKEN_URL, data=token_data)
        token_json = response.json()
        access_token = token_json.get('access_token')

        self.send_response(200)
        self.end_headers()
        self.wfile.write(b'Authentication successful. Return to the app.')

# Run server in background
def start_oauth_server():
    server = HTTPServer(('localhost', 8080), OAuthCallbackHandler)
    server.handle_request()

def get_authorization_url():
    params = {
        'response_type': 'code',
        'client_id': CLIENT_ID,
        'redirect_uri': REDIRECT_URI,
        'scope': 'r_liteprofile r_emailaddress'
    }
    return f"{AUTH_URL}?{urlencode(params)}"

# Hugging Face summarization
def summarize_profile(name, headline, email):
    text = f"{name} is a professional with expertise in: {headline}. Contact: {email}"
    response = requests.post(
        "https://api-inference.huggingface.co/models/facebook/bart-large-cnn",
        headers={"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"},
        json={"inputs": text}
    )
    return response.json()[0]["summary_text"]

def linkedIn_summary_flow():
    global access_token
    # Trigger OAuth
    threading.Thread(target=start_oauth_server).start()
    return f"[Click here to authorize LinkedIn]({get_authorization_url()})"

def fetch_and_summarize(_):
    headers = {"Authorization": f"Bearer {access_token}"}
    profile = requests.get(PROFILE_API, headers=headers).json()
    email_data = requests.get(EMAIL_API, headers=headers).json()

    name = profile.get("localizedFirstName", "") + " " + profile.get("localizedLastName", "")
    headline = profile.get("localizedHeadline", "")
    email = email_data['elements'][0]['handle~']['emailAddress']

    summary = summarize_profile(name, headline, email)
    return f"**Name:** {name}\n**Headline:** {headline}\n**Email:** {email}\n\n**Summary:** {summary}"

with gr.Blocks() as demo:
    gr.Markdown("# πŸ”— LinkedIn Profile Summarizer")
    gr.Markdown("1. Click to authorize via LinkedIn\n2. Fetch & summarize your profile")

    btn_auth = gr.Button("πŸ” Connect to LinkedIn")
    btn_fetch = gr.Button("πŸ“„ Summarize My Profile")
    output = gr.Textbox(label="Summary", lines=10)

    btn_auth.click(linkedIn_summary_flow, outputs=output)
    btn_fetch.click(fetch_and_summarize, inputs=[], outputs=output)

demo.launch()