tkgauri commited on
Commit
bf1fdc6
Β·
verified Β·
1 Parent(s): 82f37b0

Linkedin connect

Browse files
Files changed (1) hide show
  1. app.py +95 -63
app.py CHANGED
@@ -1,64 +1,96 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
-
63
- if __name__ == "__main__":
64
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()