how to read information about ontology using owlready2 - python

I am working in flask app and want to load ontology and print how many classes and how many individuals in this ontology
here is my code, it does not work
import flask
from owlready2 import *
app = flask.Flask(__name__)
app.config["DEBUG"] = True
#app.route('/', methods=['GET'])
def start():
onto_path.append("pizza.owl")
onto = get_ontology("pizza.owl")
onto.load()
print(onto.classes())
print(list(onto.individuals()))
html = ''
html += '<h2>clases: ' + onto.classes() + '</br>'
html += '<h3>individuals: ' + onto.individuals()
return html
#return "<h1>Distant Reading Archive</h1><p>This site is a prototype API for distant reading of science fiction novels.</p>"
app.run()

The method classes() and individuals() returns a generator, so you should cast the generator into a list, and ask for the length of that object.
n_classes = len(list(onto.classes()))
n_individuals = len(list(onto.individuals()))
After that, you should have the numbers on your variables and you can concatenate them with your HTML.

Related

How to return multiple values from this program to API host server

This is a program that scraps data from an internship site and then converts it as a rest API which posts data as JSON to the local server
NOTE: - This is a part of the whole program which is functional on its own.
import scrapped_intershala
from flask import Flask, jsonify
import json
import requests
import webbrowser
from bs4 import BeautifulSoup
start_link='https://internshala.com'
html_text = requests.get('https://internshala.com/internships/game%20development-internship').text #for game development
soup= BeautifulSoup (html_text,'lxml')
app = Flask(__name__)
#app.route('/')
def run():
company_name= soup.find_all('div', class_="heading_4_5 profile")
stipend = soup.find_all(('span'), class_="stipend")
location = soup.find_all(('a'), class_="location_link")
start_date = soup.find_all(('div'), class_="item_body", id="start-date-first")
link_to = soup.find_all(('div'),class_="heading_4_5 profile")
for name, sal, loc, Sdate, LK in zip(company_name, stipend, location, start_date, link_to): #zip takes iterable or containers and returns a single iterator object, having mapped values from all the containers.
#name below are only here as these need formatting
nm = name.text.replace('\n','') #name of company
dt = Sdate.text.replace('\n','').replace('Immediately','').strip() #date of joining as intern
#name above are only here as these need formatting
answer={
'Company name' : nm,
'Stipend' : sal.text,
'Location': loc.text,
'Date of joining as intern' : dt,
'For more information link for internship': start_link+LK.a['href']
}
queries_json = json.dumps(answer)
return queries_json
#print(f'''Company name: {nm}\n\nStipend: {sal.text}\n\nLocation: {loc.text}\n\nDate of joining as intern: {dt}\n\nFor more information link for internship: {start_link+LK.a['href']}\n\n\n\n\n\n''')
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=7000) #this command may chance for every machine / / here host 0.0.0.0 is used as this machine uses a private IP
This program only returns one value to the local server rather than returning all values to the server
return statement in python breaks the execution so if you use it in a for loop it break the loop.
What you should do is use the foor loop to create a list object and then return it instead:
all_answers = []
for item in ...:
answer = {...}
all_answers.append(answer)
return json.dumps(all_answers)

Flask API too slow.. only serving 20 requests per sec? [duplicate]

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 3 years ago.
My Flask API which deploys keras model is too slow. It only serves 180 requests in 10 seconds.
The API has 3 .py files.
server_side.py:
def predict():
if request.method == "POST":
comment = request.form["comment"]
movies = [int(x) for x in comment.split(",")]
movies, g_input, t_input, movie_copy = prepare_inputs(
movies)
#print(movies[:10],g_input,t_input)
preds = make_prediction(movies, g_input, t_input, MODEL)
most_similar = preds[0].argsort()[-(10 + len(movie_copy)) :][::-1]
watched_movies, rec_movies = result(movie_copy, most_similar, DF)
context = {
"table1": [watched_movies.to_html(classes="data")],
"title1": watched_movies.columns.values,
"table2": [rec_movies.to_html(classes="data")],
"title2": rec_movies.columns.values,
}
return render_template("prediction.html", context=context)
if __name__ == "__main__":
global MODEL
MODEL = MODEL
app.run(threaded=False)
prediction_helper.py:
has two methods, one to predict and another to prepare inputs to the required length and shape.
def make_prediction(movies, g_input, t_input, model):
return model.predict(
[np.array([movies,]), np.array([g_input,]), np.array([t_input,])]
)
def prepare_inputs(movie_input):
movies_copy = movie_input[:]
genres = ' '.join(DF.genres[DF["movieId"].isin(movie_input)].values)
genres = genres.split(" ")
tags = ' '.join(DF.tag[DF["movieId"].isin(movie_input)].values)
tags = tags.split(" ")
genres = [
list(GENRES_MAP.keys())[list(GENRES_MAP.values()).index(i)] for i in genres
]
genres = list(set(genres))
genres = pad(genres, INPUT_LENGTH["genre_len"])
tags = [
list(TAG_MAP.keys())[list(TAG_MAP.values()).index(i)]
for i in tags
if i not in ("em", "cs", "se")
]
tags = list(set(tags))
if len(tags) > 100:
tags = tags[0:100]
else:
tags = pad(tags, INPUT_LENGTH["tag_len"])
movie_input = pad(movie_input, INPUT_LENGTH["movie_len"])
return movie_input, genres, tags, movies_copy
data_loader.py:
has two methods, pad which pads a list to the required length and another which extracts rows based on the prediction indices.
import pickle
import pandas as pd
from keras.models import load_model
MODEL = load_model("final_train1.h5")
DF = pd.read_csv("new_df.csv")
DF["tag"] = DF["tag"].fillna("")
PICKLE_IN = open("genres_map.pickle", "rb")
GENRES_MAP = pickle.load(PICKLE_IN)
PICKLE_IN = open("tag_map.pickle", "rb")
TAG_MAP = pickle.load(PICKLE_IN)
INPUT_LENGTH = {"movie_len":2698, "genre_len":24, "tag_len":100}
def pad(lst, width):
lst.extend([0] * (width - len(lst)))
return lst
def result(movie_copy, most_similar, DF):
rec_movies = DF.set_index("movieId").loc[most_similar]
watched_movies = DF.set_index("movieId").loc[movie_copy]
return watched_movies, rec_movies
You shouldn't use flask built-in server that just designed for development purpose and not for production deployment. Instead, you should use an external server for production deployment like Waitress, uWSGI, Gunicorn or Nginx. You can read here for the options for flask production deployment: https://flask.palletsprojects.com/en/1.1.x/deploying/
Flask documentation says the development server is not for production. Use a production WSGI server such as Gunicorn along with Nginx.

How to show data from other function into flask app

I am new to python and flask. Can anyone help me on this
from flask import Flask
import logging
logging.basicConfig(filename="err.log",level=logging.DEBUG)
student = ['abcd', '001', '11'] # I want to extract list value from here and needs to display in flask app
app = Flask(__name__)
#app.route("/")
def student_details():
print("student: "+student[0])
print("id: "+student[1])
print("grade: "+student[2])
student_details()
if __name__ == "__main__":
app.run(host='127.0.0.1', port="8000",debug=False)
Current Output: Internal Server Error
Desired Output:
student: abcd
id: 001
grade: 11
Although I would suggest reading about how to make html pages and use Jinja templates with flask, Use return instead of print. print() is used for printing on the console. You can also use a for loop to generate a string and then return it.
from flask import Flask
import logging
logging.basicConfig(filename="err.log",level=logging.DEBUG)
student = ['abcd', '001', '11'] # I want to extract list value from here and needs to display in flask app
app = Flask(__name__)
#app.route("/")
def student_details():
return "student " + str(student[0]) + " id " + str(student[1]) + " grade " + str(student[2])
if __name__ == "__main__":
app.run(host='127.0.0.1', port="8000",debug=False)

Flask server could not handle non ascii characters

I have wrriten a simple application using flask. Its main objective is to implement CLD2 (language detector) using post and get methods. It is working well for English but for any other language such Urdu, Arabic. It gives invalid results
Following is the corresponding script
# http://127.0.0.1:5000/cld2?text="Your input text string"
# OUTPUT ( It gives output as we done in CC)
#"585&URDU-99-1155"
from flask import Flask,abort,jsonify,request
from flask_restful import Resource, Api, reqparse
import cld2
from bs4 import BeautiflSoup
import sys
import urllib2, urllib
import re
reload(sys)
sys.setdefaultencoding('utf8')
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def cld2_states(self, txt):
txt = txt.encode("utf8")
isReliable, textBytesFound, details = cld2.detect(txt)
outstr = str(textBytesFound)
for item in details: # Iterate 3 languages
if item[0] != "Unknown":
outstr += '&' + item[0] + '-' + str(item[2]) + '-' + str(int(item[3]))
return outstr
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('text', type=str)
parser.add_argument('url', type=str)
_dict = dict(parser.parse_args())
if _dict["text"] is not None:
value = _dict["text"]
print type(value)
return self.cld2_states(value)
return None
def post(self):
data = request.get_json(force=True)
# print data
predict_request = [data['content']][1]
out = self.cld2_states(predict_request)
return jsonify(score=out)
api.add_resource(HelloWorld, '/cld2')
if __name__ == '__main__':
app.run(debug=True, port=6161, host='0.0.0.0')
If I give a query via get method, it give correct results but for same query in post method, it return just a number. But if text is in English then post also give correct result.
My client is a simple Java application then iterate over files and find their language one by one.
The problem might be with this line:
outstr = str(textBytesFound)
Instead of using str to convert from bytes to str, use str.decode(), like this:
outstr = textBytesFound.decode("utf-8")
(obviously if your text is not encoded with UTF-8, you need to tell Python the correct encoding to use)

Same code used in multiple functions but with minor differences - how to optimize?

This is the code of a Udacity course, and I changed it a little. Now, when it runs, it asks me for a movie name and the trailer would open in a pop up in a browser (that's another part, which is not shown).
As you can see, this program has a lot of repetitive code in it, the functions extract_name, movie_poster_url and movie_trailer_url have kind of the same code. Is there a way to get rid of the same code being repeated but have the same output? If so, will it run faster?
import fresh_tomatoes
import media
import urllib
import requests
from BeautifulSoup import BeautifulSoup
name = raw_input("Enter movie name:- ")
global movie_name
def extract_html(name):
url = "website name" + name + "continuation of website name" + name + "again continuation of web site name"
response = requests.get(url)
page = str(BeautifulSoup(response.content))
return page
def extract_name(page):
start_link = page.find(' - IMDb</a></h3><div class="s"><div class="kv"')
start_url = page.find('>',start_link-140)
start_url1 = page.find('>', start_link-140)
end_url = page.find(' - IMDb</a>', start_link-140)
name_of_movie = page[start_url1+1:end_url]
return extract_char(name_of_movie)
def extract_char(name_of_movie):
name_array = []
for words in name_of_movie:
word = words.strip('</b>,')
name_array.append(word)
return ''.join(name_array)
def movie_poster_url(name_of_movie):
movie_name, seperator, tail = name_of_movie.partition(' (')
#movie_name = name_of_movie.rstrip('()0123456789 ')
page = urllib.urlopen('another web site name' + movie_name + 'continuation of website name').read()
start_link = page.find('"Poster":')
start_url = page.find('"',start_link+9)
end_url = page.find('"',start_url+1)
poster_url = page[start_url+1:end_url]
return poster_url
def movie_trailer_url(name_of_movie):
movie_name, seperator, tail = name_of_movie.partition(' (')
#movie_name = name_of_movie.rstrip('()0123456789 ')
page = urllib.urlopen('another website name' + movie_name + " trailer").read()
start_link = page.find('<div class="yt-lockup-dismissable"><div class="yt-lockup-thumbnail contains-addto"><a aria-hidden="true" href=')
start_url = page.find('"',start_link+110)
end_url = page.find('" ',start_url+1)
trailer_url1 = page[start_url+1:end_url]
trailer_url = "www.youtube.com" + trailer_url1
return trailer_url
page = extract_html(name)
movie_name = extract_name(page)
new_movie = media.Movie(movie_name, "Storyline WOW", movie_poster_url(movie_name), movie_trailer_url(movie_name))
movies = [new_movie]
fresh_tomatoes.open_movies_page(movies)
You could move the shared parts into their own function:
def find_page(url, name, find, offset):
movie_name, seperator, tail = name_of_movie.partition(' (')
page = urllib.urlopen(url.format(name)).read()
start_link = page.find(find)
start_url = page.find('"',start_link+offset)
end_url = page.find('" ',start_url+1)
return page[start_url+1:end_url]
def movie_poster_url(name_of_movie):
return find_page("another website name{} continuation of website name", name_of_movie, '"Poster":', 9)
def movie_trailer_url(name_of_movie):
trailer_url = find_page("another website name{} trailer", name_of_movie, '<div class="yt-lockup-dismissable"><div class="yt-lockup-thumbnail contains-addto"><a aria-hidden="true" href=', 110)
return "www.youtube.com" + trailer_url
It definetely wont run faster (there is extra work to do to "switch" between the functions) but the performance difference is probably negligable.
For your second question: Profiling is not a technique or method, it's "finding out what's being bad" in your code:
Profiling is a form of
dynamic program analysis that measures, for example, the space
(memory) or time complexity of a program, the usage of particular
instructions, or the frequency and duration of function calls.
(wikipedia)
So it's not something that speeds up your program, it's a word for things you do to find out what you can do to speed up your program.
Going really quickly here because I am a super newb but I can see the repetition; what I would do is to figure out the (mostly) repeating blocks of code shared by all 3 functions and then figure out where they differ; write a new function that takes the differences as the arguments. so for instance:
def extract(tarString,delim,startDiff,endDiff):
start_link = page.find(tarString)
start_url = page.find(delim,start_link+startDiff)
end_url = page.find(delim,start_url+endDiff)
url_out = page[start_url+1:end_url]
Then, in your poster, trailer, etc functions, just call this extract function with the appropriate arguments for each case. ie poster would call
poster_url=extract(tarString='"Poster:"',delim='"',startDiff=9, endDiff=1)
I can see you've got another answer already and it's very likely it's written by someone who knows more than I do, but I hope you get something out of my "philosophy of modularizing" from a newbie perspective.

Categories

Resources