Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +3 -9
- app.py +62 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji: 🐢
|
| 4 |
-
colorFrom: pink
|
| 5 |
-
colorTo: blue
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.39.0
|
| 8 |
app_file: app.py
|
| 9 |
-
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: keyword_paste
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app_file: app.py
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 4.37.2
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cohere
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Initialize Cohere client using API key from environment variable
|
| 7 |
+
co = cohere.Client(os.getenv('COHERE_API_KEY'))
|
| 8 |
+
|
| 9 |
+
def calculate_relevancy(meta_title, h1_heading, first_paragraph, body, keywords):
|
| 10 |
+
# Generate embeddings for each part
|
| 11 |
+
response_meta_title = co.embed(texts=[meta_title])
|
| 12 |
+
meta_title_embedding = response_meta_title.embeddings[0]
|
| 13 |
+
|
| 14 |
+
response_h1_heading = co.embed(texts=[h1_heading])
|
| 15 |
+
h1_heading_embedding = response_h1_heading.embeddings[0]
|
| 16 |
+
|
| 17 |
+
response_first_paragraph = co.embed(texts=[first_paragraph])
|
| 18 |
+
first_paragraph_embedding = response_first_paragraph.embeddings[0]
|
| 19 |
+
|
| 20 |
+
response_body = co.embed(texts=[body])
|
| 21 |
+
body_embedding = response_body.embeddings[0]
|
| 22 |
+
|
| 23 |
+
# Generate embeddings for each keyword
|
| 24 |
+
keyword_list = [kw.strip() for kw in keywords.split(",")]
|
| 25 |
+
response_keywords = co.embed(texts=keyword_list)
|
| 26 |
+
keyword_embeddings = response_keywords.embeddings
|
| 27 |
+
|
| 28 |
+
# Calculate relevancy score (cosine similarity) for each part
|
| 29 |
+
relevancy_scores = []
|
| 30 |
+
for kw, kw_embedding in zip(keyword_list, keyword_embeddings):
|
| 31 |
+
scores = [
|
| 32 |
+
kw,
|
| 33 |
+
np.dot(meta_title_embedding, kw_embedding) / (np.linalg.norm(meta_title_embedding) * np.linalg.norm(kw_embedding)),
|
| 34 |
+
np.dot(h1_heading_embedding, kw_embedding) / (np.linalg.norm(h1_heading_embedding) * np.linalg.norm(kw_embedding)),
|
| 35 |
+
np.dot(first_paragraph_embedding, kw_embedding) / (np.linalg.norm(first_paragraph_embedding) * np.linalg.norm(kw_embedding)),
|
| 36 |
+
np.dot(body_embedding, kw_embedding) / (np.linalg.norm(body_embedding) * np.linalg.norm(kw_embedding))
|
| 37 |
+
]
|
| 38 |
+
relevancy_scores.append(scores)
|
| 39 |
+
return relevancy_scores
|
| 40 |
+
|
| 41 |
+
# Create Gradio interface
|
| 42 |
+
def gradio_interface():
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
with gr.Row():
|
| 45 |
+
meta_title_input = gr.Textbox(label="Meta Title", placeholder="Enter Meta Title here")
|
| 46 |
+
h1_heading_input = gr.Textbox(label="H1 Heading", placeholder="Enter H1 Heading here")
|
| 47 |
+
first_paragraph_input = gr.Textbox(label="First Paragraph", placeholder="Enter First Paragraph here")
|
| 48 |
+
body_input = gr.Textbox(label="Body", placeholder="Enter Body here", lines=5)
|
| 49 |
+
keywords_input = gr.Textbox(label="Keywords", placeholder="Enter keywords separated by commas")
|
| 50 |
+
|
| 51 |
+
relevancy_button = gr.Button("Calculate Relevancy")
|
| 52 |
+
relevancy_output = gr.Dataframe(headers=["Keyword", "Meta Title", "H1 Heading", "First Paragraph", "Body"])
|
| 53 |
+
|
| 54 |
+
relevancy_button.click(calculate_relevancy,
|
| 55 |
+
inputs=[meta_title_input, h1_heading_input, first_paragraph_input, body_input, keywords_input],
|
| 56 |
+
outputs=relevancy_output)
|
| 57 |
+
|
| 58 |
+
return demo
|
| 59 |
+
|
| 60 |
+
# Launch Gradio app
|
| 61 |
+
demo = gradio_interface()
|
| 62 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
cohere
|
| 3 |
+
numpy
|