|
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) |
|
nutri.append(json.dumps(nutritional_info, indent=4)) |
|
|
|
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+=row['price'] + "\n" |
|
|
|
return menu |
|
|
|
def reply_bot(message, history): |
|
client = InferenceClient(model="https://9347-141-3-25-29.ngrok-free.app") |
|
|
|
prompt = "<s>[INST] <<SYS>>\nYou are multilingual chat bot that helps deciding what to eat ina german canteen. In the canteen there are different lines with names. 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\n<</SYS>>\n\nMenu:\n" + menu + "\n " + histroy + "\n" + message + " [/INST]" |
|
|
|
try: |
|
for token in client.text_generation(prompt=prompt, max_new_tokens=128, stream=True): |
|
yield token |
|
except: |
|
return "Clear History or ask FR to increase Context Window. Current capacity only 4k tokens" |
|
|
|
gr.ChatInterface(reply_bot).launch() |
|
|