Spaces:
Runtime error
Runtime error
basic app
Browse files- app.py +94 -4
- requirements.txt +8 -0
app.py
CHANGED
@@ -1,7 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
iface.launch()
|
|
|
1 |
+
from sentence_transformers import SentenceTransformer
|
2 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
3 |
+
|
4 |
+
import nltk
|
5 |
+
nltk.download('all')
|
6 |
+
from nltk.corpus import wordnet as wn
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
import gradio as gr
|
10 |
+
import pyjokes
|
11 |
+
|
12 |
+
iface = gr.Interface(fn=generator, inputs="text", outputs="text")
|
13 |
+
iface.launch()
|
14 |
+
|
15 |
+
|
16 |
+
def similarity(input, joke):
|
17 |
+
return cosine_similarity(input, joke)
|
18 |
+
|
19 |
+
|
20 |
+
def get_best(input):
|
21 |
+
model = SentenceTransformer('bert-base-nli-mean-tokens')
|
22 |
+
max_similarity = -1
|
23 |
+
max_idx = 0
|
24 |
+
jokes = pyjokes.get_jokes(language='en', category='all')
|
25 |
+
jokes_embedding = model.encode(jokes)
|
26 |
+
input_embedding = model.encode(input)
|
27 |
+
for idx, joke_embedding in enumerate(jokes_embedding):
|
28 |
+
sim = similarity(joke_embedding.reshape(-1, 1), input_embedding.reshape(-1,1))
|
29 |
+
if(np.sum(sim) > np.sum(max_similarity)):
|
30 |
+
max_idx = idx
|
31 |
+
max_similarity = sim
|
32 |
+
if(np.sum(max_similarity) != -1):
|
33 |
+
return jokes[max_idx]
|
34 |
+
else:
|
35 |
+
return None
|
36 |
+
|
37 |
+
def generate_list(input):
|
38 |
+
result = []
|
39 |
+
n = len(input)
|
40 |
+
for Len in range(2, n + 1):
|
41 |
+
for i in range(n - Len + 1):
|
42 |
+
j = i + Len - 1
|
43 |
+
tem = ""
|
44 |
+
for k in range(i, j + 1):
|
45 |
+
tem += input[k]
|
46 |
+
result.append(tem)
|
47 |
+
return result
|
48 |
+
|
49 |
+
|
50 |
+
def pattern(input):
|
51 |
+
response = input
|
52 |
+
for substr in generate_list(input):
|
53 |
+
try :
|
54 |
+
syn = wn.synsets(substr)[1].hypernyms()[0].hyponyms()[0].hyponyms()[0].lemmas()[0].name()
|
55 |
+
except:
|
56 |
+
continue
|
57 |
+
if(syn != None):
|
58 |
+
response = response.replace(substr, syn.upper())
|
59 |
+
|
60 |
+
if(input == response):
|
61 |
+
return None
|
62 |
+
else :
|
63 |
+
return response
|
64 |
+
|
65 |
+
|
66 |
+
def GPT(input):
|
67 |
+
return None
|
68 |
+
|
69 |
+
|
70 |
+
def generator(input=None):
|
71 |
+
response = []
|
72 |
+
if input:
|
73 |
+
|
74 |
+
out1 = GPT(input)
|
75 |
+
if(out1):
|
76 |
+
for out in out1:
|
77 |
+
response.append(out)
|
78 |
+
|
79 |
+
out2 = pattern(input)
|
80 |
+
if(out2):
|
81 |
+
response.append(out2)
|
82 |
+
|
83 |
+
out3 = get_best(input)
|
84 |
+
if(out3):
|
85 |
+
response.append(out3)
|
86 |
+
|
87 |
+
else:
|
88 |
+
out1 = GPT("Hi, what's the matter")
|
89 |
+
if(out1):
|
90 |
+
for out in out1:
|
91 |
+
response.append(out)
|
92 |
|
93 |
+
out2 = pyjokes.get_joke(language='en', category='all')
|
94 |
+
if(out2):
|
95 |
+
response.append(out2)
|
96 |
|
97 |
+
return response #[0] think of doing this
|
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
sentence_transformers==2.2.2
|
2 |
+
sklearn==1.0.2
|
3 |
+
nltk==3.7
|
4 |
+
gradio==3.2
|
5 |
+
pyjokes==0.6.0
|
6 |
+
fastai==2.7.9
|
7 |
+
pandas==1.3.5
|
8 |
+
numpy==1.21.6
|