initial commit
Browse files- README.md +5 -5
- app.py +42 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 3.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Gpt 2 Secret
|
| 3 |
+
emoji: 😻
|
| 4 |
+
colorFrom: pink
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 3.39.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from transformers import pipeline, set_seed
|
| 4 |
+
from transformers import AutoTokenizer, AutoModel
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def mean_pooling(model_output, attention_mask):
|
| 10 |
+
token_embeddings = model_output[0]
|
| 11 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 12 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
| 17 |
+
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def Bemenet(bemenet):
|
| 22 |
+
# Tokenize sentences
|
| 23 |
+
encoded_input = tokenizer([bemenet], padding=True, truncation=True, return_tensors='pt')
|
| 24 |
+
|
| 25 |
+
# Compute token embeddings
|
| 26 |
+
with torch.no_grad():
|
| 27 |
+
model_output = model(**encoded_input)
|
| 28 |
+
|
| 29 |
+
# Perform pooling
|
| 30 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
| 31 |
+
|
| 32 |
+
# Normalize embeddings
|
| 33 |
+
return F.normalize(sentence_embeddings, p=2, dim=1)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
interface = gr.Interface(fn=Bemenet,
|
| 37 |
+
title="Cím..",
|
| 38 |
+
description="Leírás..",
|
| 39 |
+
inputs="text",
|
| 40 |
+
outputs="text")
|
| 41 |
+
|
| 42 |
+
interface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
tensorflow
|
| 3 |
+
torch
|