skoneru commited on
Commit
99573a1
·
1 Parent(s): af51c89

Add application file

Browse files
Files changed (1) hide show
  1. app.py +129 -0
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import wget
3
+ import sys
4
+ import bs4
5
+ import json
6
+ import pandas as pd
7
+ from huggingface_hub import InferenceClient
8
+ import urllib.request
9
+ import gradio as gr
10
+
11
+ def get_menu():
12
+ fp = urllib.request.urlopen("https://www.sw-ka.de/en/hochschulgastronomie/speiseplan/mensa_adenauerring/")
13
+ mybytes = fp.read()
14
+
15
+ html_content = mybytes.decode("utf8")
16
+
17
+ #html_content = "".join(open('index.html',mode='r',encoding='utf-8').readlines())
18
+ # Parse the HTML content
19
+
20
+ # Parse the HTML using BeautifulSoup
21
+ soup = bs4.BeautifulSoup(html_content, 'html.parser')
22
+
23
+ canteen_div = soup.find('div', id='canteen_day_1')
24
+ # Find all tables within the canteen_div
25
+ tables = canteen_div.find_all('table')
26
+
27
+ foods = []
28
+ prices = []
29
+ nutri = []
30
+ line_names = []
31
+
32
+
33
+
34
+ cnt = 0
35
+ canteen_div = soup.find('div', id='canteen_day_1')
36
+ # Find all tables within the canteen_div
37
+ tables = canteen_div.find_all('table')
38
+ # Iterate over each table
39
+ for table in tables:
40
+ # Extract food name
41
+ # Find all table rows with a class starting with "mt-"
42
+ menu_items = table.find_all('tr', class_=lambda class_name: class_name and class_name.startswith('mt-'))
43
+
44
+ # Iterate through each menu item
45
+ for item in menu_items:
46
+ food_name = item.find('span', class_='bg').text.strip()
47
+
48
+ # Extract price
49
+ price = item.find('span', class_='bgp price_1').text.strip()
50
+
51
+ # Extract nutritional information
52
+ nutritional_info = {}
53
+ nutritional_data = item.find('div', class_='nutrition_facts')
54
+ if nutritional_data:
55
+ for element in nutritional_data.find_all('div', class_=['energie', 'proteine', 'kohlenhydrate', 'zucker', 'fett', 'gesaettigt', 'salz']):
56
+ key = element.find('div').text.strip()
57
+ value = element.find_all('div')[1].text.strip()
58
+ nutritional_info[key] = value
59
+
60
+ # Print extracted information
61
+
62
+ #print(f"\nFood Name: {food_name}")
63
+ foods.append(food_name)
64
+ prices.append(price)
65
+ nutri.append(json.dumps(nutritional_info, indent=4))
66
+ #print(f"Price: {price}")
67
+ if nutritional_info:
68
+ #print("Nutritional Information:")
69
+ for key, value in nutritional_info.items():
70
+ pass
71
+ #print(f"- {key}: {value}")
72
+ else:
73
+ pass
74
+ #print("No nutritional information available.")
75
+ cnt+=1
76
+ break
77
+ # Iterate over each row
78
+ # Find all rows (tr) with class 'mensatype_rows'
79
+ #rows = table.find_all('tr', class_='mensatype_rows')
80
+ # Find all menu items within the table
81
+
82
+ # Find all rows with class 'mensatype_rows'
83
+ canteen_div = soup.find('div', id='canteen_day_1')
84
+ # Find all tables within the canteen_div
85
+ tables = canteen_div.find_all('table')
86
+
87
+
88
+ # Iterate over each table
89
+ for table in tables:
90
+ # Iterate over each row
91
+ # Find all rows (tr) with class 'mensatype_rows'
92
+ rows = table.find_all('tr', class_='mensatype_rows')
93
+
94
+ # Iterate over each row
95
+ for row in rows:
96
+ # Extract the row name
97
+ row_name = row.find('div').get_text(strip=True)
98
+ menu_titles = row.find_all('td', class_='menu-title')
99
+
100
+ # Iterate over each food item
101
+ for menu_title in menu_titles:
102
+ line_names.append(row_name)
103
+
104
+ menu = ""
105
+ df = pd.DataFrame(zip(line_names,foods,prices,nutri),columns=['line','food','price','nutri'])
106
+ #df = df[~df['line'].str.contains("Abendessen")]
107
+ #df = df[~df['line'].str.contains("pizza")]
108
+ #df = df[~df['line'].str.contains("werk")]
109
+ df_line = df.groupby('line', sort=False)
110
+ for line, df_group in df_line:
111
+ menu+= "Line Name: " + line + "\n"
112
+ for idx,row in df_group.iterrows():
113
+ menu+=row['food'] + "\n"
114
+ menu+=row['price'] + "\n"
115
+ #menu+=row['nutri'] + "\n"
116
+ return menu
117
+
118
+ def reply_bot(message, history):
119
+ client = InferenceClient(model="https://9347-141-3-25-29.ngrok-free.app")
120
+
121
+ 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]"
122
+
123
+ try:
124
+ for token in client.text_generation(prompt=prompt, max_new_tokens=128, stream=True):
125
+ yield token
126
+ except:
127
+ return "Clear History or ask FR to increase Context Window. Current capacity only 4k tokens"
128
+
129
+ gr.ChatInterface(reply_bot).launch()