File size: 2,796 Bytes
ac623be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import requests
from PIL import Image
import io
import base64

# FILTER

import re
def clean_string(input_string):
    # Lowercase the string and remove non-alphabetic characters
    cleaned_string = re.sub(r'[^a-z]', '', input_string.lower())
    return cleaned_string
with open("invalid_searches.txt", 'r') as f:
	invalid = f.readlines()
	invalid = [clean_string(i) for i in invalid]
	invalid = [i for i in invalid if i != ""]
	invalid.append("")
def check_invalid(word):
	word = clean_string(word)
	return word in invalid

# GETTING THE KEYS

# from keys import *
import os
HF_TOKEN = os.environ.get('HF_TOKEN')
DICTIONARY_API_KEY = os.environ.get('DICTIONARY_API_KEY')

def get_definition(word):

	url = 'https://siwar.ksaa.gov.sa/api/alriyadh/exact-search'
	headers = {
		'accept': 'application/json',
		'apikey': DICTIONARY_API_KEY
	}
	params = {'query': word}
	response = requests.get(url, params=params, headers=headers)
	print(response)

	# word not found
	define = response.json()
	if len(define) == 0:
		return "", "",  []

	define = define[0]

	word = define['lemma']['formRepresentations'][0]['form']
	english = None
	meanings_examples = []

	for i, sense in enumerate(define['senses']):
		meaning = sense['definition']['textRepresentations'][0]['form']
		example = None
		for ex in sense['examples']:
			if ex['form'] != "":
				example = ex['form']
		for ex in sense['translations']:
			if ex['form'] != "" and not english:
				english = ex['form']

		meanings_examples.append({
			"i": i+1,
			"meaning": meaning,
			"example": example
		})

	return word, english, meanings_examples

def get_translation(word):

	if word == "":
		return ""

	API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-ar-en"
	headers = {"Authorization": f"Bearer {HF_TOKEN}"}

	response = requests.post(API_URL, headers=headers, json={
		"inputs": word,
	})
	print(response)
	response = response.json()
	print(response)

	# error in response
	if type(response) == dict:
		return ""

	return response[0]['translation_text']

def get_image(word):

	print(word)

	if check_invalid(word):
		return None
		# blank image
		return "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="

	API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
	headers = {"Authorization": f"Bearer {HF_TOKEN}"}

	response = requests.post(API_URL, headers=headers, json={
		"inputs": word,
	})
	print(response)
	image_bytes = response.content

	image = Image.open(io.BytesIO(image_bytes))

	# Convert image to base64
	image_base64 = ""
	with io.BytesIO() as buffer:
		image.save(buffer, format="JPEG")
		image_base64 = base64.b64encode(buffer.getvalue()).decode('utf-8')
	
	return image_base64


if __name__ == "__main__":
	pass