|
import os |
|
import wget |
|
import sys |
|
import bs4 |
|
import json |
|
import pandas as pd |
|
from huggingface_hub import InferenceClient |
|
import urllib.request |
|
import gradio as gr |
|
|
|
def get_menu(): |
|
fp = urllib.request.urlopen("https://www.sw-ka.de/en/hochschulgastronomie/speiseplan/mensa_adenauerring/") |
|
mybytes = fp.read() |
|
|
|
html_content = mybytes.decode("utf8") |
|
|
|
|
|
|
|
|
|
|
|
soup = bs4.BeautifulSoup(html_content, 'html.parser') |
|
|
|
canteen_div = soup.find('div', id='canteen_day_1') |
|
|
|
tables = canteen_div.find_all('table') |
|
|
|
foods = [] |
|
prices = [] |
|
nutri = [] |
|
line_names = [] |
|
|
|
|
|
|
|
cnt = 0 |
|
canteen_div = soup.find('div', id='canteen_day_1') |
|
|
|
tables = canteen_div.find_all('table') |
|
|
|
for table in tables: |
|
|
|
|
|
menu_items = table.find_all('tr', class_=lambda class_name: class_name and class_name.startswith('mt-')) |
|
|
|
|
|
for item in menu_items: |
|
food_name = item.find('span', class_='bg').text.strip() |
|
|
|
|
|
price = item.find('span', class_='bgp price_1').text.strip() |
|
|
|
|
|
nutritional_info = {} |
|
nutritional_data = item.find('div', class_='nutrition_facts') |
|
if nutritional_data: |
|
for element in nutritional_data.find_all('div', class_=['energie', 'proteine', 'kohlenhydrate', 'zucker', 'fett', 'gesaettigt', 'salz']): |
|
key = element.find('div').text.strip() |
|
value = element.find_all('div')[1].text.strip() |
|
nutritional_info[key] = value |
|
|
|
|
|
|
|
|
|
foods.append(food_name) |
|
prices.append(price) |
|
try: |
|
nutri.append(json.dumps(nutritional_info['Energie'], indent=4)) |
|
except: |
|
nutri.append("") |
|
|
|
if nutritional_info: |
|
|
|
for key, value in nutritional_info.items(): |
|
pass |
|
|
|
else: |
|
pass |
|
|
|
cnt+=1 |
|
break |
|
|
|
|
|
|
|
|
|
|
|
|
|
canteen_div = soup.find('div', id='canteen_day_1') |
|
|
|
tables = canteen_div.find_all('table') |
|
|
|
|
|
|
|
for table in tables: |
|
|
|
|
|
rows = table.find_all('tr', class_='mensatype_rows') |
|
|
|
|
|
for row in rows: |
|
|
|
row_name = row.find('div').get_text(strip=True) |
|
menu_titles = row.find_all('td', class_='menu-title') |
|
|
|
|
|
for menu_title in menu_titles: |
|
line_names.append(row_name) |
|
|
|
menu = "" |
|
df = pd.DataFrame(zip(line_names,foods,prices,nutri),columns=['line','food','price','nutri']) |
|
|
|
|
|
|
|
df_line = df.groupby('line', sort=False) |
|
for line, df_group in df_line: |
|
menu+= "Line Name: " + line + "\n" |
|
for idx,row in df_group.iterrows(): |
|
menu+=row['food'] + "\n" |
|
menu+= "Price: " + row['price'] + "\n" |
|
menu+= "Calories: " + row['nutri'] + "\n" |
|
return menu |
|
|
|
def reply_bot(message, history): |
|
menu = get_menu() |
|
|
|
client = InferenceClient(model="https://8cc9-141-3-25-29.ngrok-free.app") |
|
|
|
system_prompt = "<s>[INST] <<SYS>>\nYou are multilingual chat bot that helps deciding what to eat in a german canteen. In the canteen, there are different lines with names and each line may offer several food people can choose from or only one. Based on the menu and question, you suggest the user which line they should go to. You respond really briefly and do not generate long responses. You can only suggest them from the menu and which line they can go to. Nothing else!\n<</SYS>>\n\nMenu:\n" + menu + "\n" |
|
|
|
curr_prompt = system_prompt + message + " [/INST]" |
|
if len(history) != 0: |
|
for human, ai in history: |
|
system_prompt += human + " [/INST]" + ai + "<s>[INST]\n" |
|
|
|
curr_prompt = system_prompt + message + " [/INST]" |
|
else: |
|
curr_prompt = "<s>[INST] <<SYS>>\nYou are multilingual chat bot that helps deciding what to eat in a german canteen. In the canteen, there are different lines with names and each line may offer several food people can choose from or only one. Based on the menu and question, you suggest the user which line they should go to. You respond really briefly and do not generate long responses. You can only suggest them from the menu and which line they can go to. Nothing else!\n<</SYS>>\n\nMenu:\n" + menu + "\n" + message + " [/INST]" |
|
|
|
try: |
|
print(curr_prompt) |
|
|
|
answer = "" |
|
for token in client.text_generation(prompt=curr_prompt, max_new_tokens=512, stream=True): |
|
answer+=token |
|
yield answer |
|
except: |
|
return "Clear History or ask FR to increase Context Window. Current capacity only 4k tokens" |
|
|
|
|
|
|
|
gr.ChatInterface(reply_bot).launch() |
|
|