Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sethu dev #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
nlp_backend/assets/data.csv
nlp_backend/assets/cleanedDF.csv
nlp_backend/__pycache__/__init__.cpython-310.pyc
nlp_backend/__pycache__/settings.cpython-310.pyc
nlp_backend/__pycache__/views.cpython-310.pyc
nlp_backend/__pycache__/wsgi.cpython-310.pyc
nlp_backend/__pycache__/urls.cpython-310.pyc
17 changes: 16 additions & 1 deletion nlp_backend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@

# import pandas as pd
# from ast import literal_eval
# from tqdm.notebook import tqdm
# tqdm.pandas()
# from datetime import datetime
# from transformers import pipeline


# data_path = "../assets/cleanedDF.csv"
# df = pd.read_csv(data_path, sep='\t', converters={'doc_entities': literal_eval, 'doc_keyphrases': literal_eval})
# interesting = df.drop(['id','doc_date', 'doc_title', 'doc_url', 'doc_entities', 'doc_keyphrases', 'doc_publish_location'], axis=1)
# model_name = "deepset/roberta-base-squad2"
# fb_ai = pipeline('question-answering', model=model_name, tokenizer=model_name)
from ast import literal_eval

import pandas as pd
Expand All @@ -14,4 +28,5 @@
interesting.dropna()
interesting['sentence'] = interesting['sentence'].fillna('')
model_name = "deepset/roberta-base-squad2"
fb_ai = pipeline('question-answering', model=model_name, tokenizer=model_name)
fb_ai = pipeline('question-answering', model=model_name, tokenizer=model_name)

1 change: 1 addition & 0 deletions nlp_backend/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nlp_backend.settings')


application = get_asgi_application()
49 changes: 35 additions & 14 deletions nlp_backend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import pandas as pd
from django.http import HttpResponse
from nlp_backend import interesting, fb_ai


from django.views.decorators.csrf import csrf_exempt
from transformers import pipeline

Expand Down Expand Up @@ -104,18 +107,36 @@ def get_continent_dist_for_a_snack(snack):
africa = [" South Africa", ' Nigeria', ' Kenya', ' Ghana', ' Zimbabwe', ' Trinidad and Tobago', ' Rwanda',
' Uganda', ' Tanzania']

africa_total = 0
for i in north_america:
north_america_total += len(df[(df['sentence'].str.contains(snack)) & (df['sentence'].str.contains(i))])
for i in south_america:
south_america_total += len(df[(df['sentence'].str.contains(snack)) & (df['sentence'].str.contains(i))])
for i in europe:
europe_total += len(df[(df['sentence'].str.contains(snack)) & (df['sentence'].str.contains(i))])
for i in africa:
africa_total += len(df[(df['sentence'].str.contains(snack)) & (df['sentence'].str.contains(i))])
asia_total = len(df[(df['sentence'].str.contains(snack)) & (df['sentence'].str.contains(' Asia'))])
answer = north_america_total, south_america_total, europe_total, asia_total, africa_total
# scale the data to total to 1
# make sure the answers are split into percentages of the total so that they can be compared
percent = [i * 100 / sum(answer) for i in answer]


def return_highest_snack_country(request,snack):
if request.method == 'GET':
country = find_snack_highest_talked_country(snack)
return HttpResponse(json.dumps(country), content_type='application/json')


# topic - "Walnuts"
# question - "What are some famous walnut types?"
# answer - "Organic Walnuts and Organic Walnuts with Apple Cinnamon"
def q_a_facebook(request, topic, question):
model_name = "deepset/roberta-base-squad2"
fb_ai = pipeline('question-answering', model=model_name, tokenizer=model_name)
sentences_topic = ' '.join(interesting[interesting['sentence'].str.contains(topic)]['sentence'])







# Find highest talked of country
def find_snack_highest_talked_country(snack):
countries = {}
for country in df['doc_publish_location'].unique():
countries[country] = df[df['doc_publish_location'] == country]['sentence'].str.contains(snack).sum()

# Find the country that has the highest count
return max(countries, key=countries.get)
* 100 / sum(answer) for i in answer]
return percent

4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Django~=4.1.3
transformers~=4.24.0
pandas~=1.5.1
tqdm~=4.64.1