Update run.py
Browse files
run.py
CHANGED
@@ -3,7 +3,66 @@ import os
|
|
3 |
import gradio as gr
|
4 |
import json
|
5 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
|
|
|
|
|
|
|
|
|
|
7 |
path='/Users/thiloid/Desktop/LSKI/ole_nest/Chatbot/LLM/chroma'
|
8 |
if(os.path.exists(path)==False): path="/home/user/app/chroma"
|
9 |
|
@@ -109,13 +168,38 @@ def parse_for_nc(text):
|
|
109 |
return "No"
|
110 |
|
111 |
|
112 |
-
def
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
def responsecritical(
|
121 |
prompt, temperature=0.9, max_new_tokens=200, top_p=0.95, repetition_penalty=1.0,
|
@@ -149,7 +233,7 @@ def responsecritical(
|
|
149 |
|
150 |
|
151 |
def response(
|
152 |
-
prompt, temperature=0.9, max_new_tokens=500, top_p=0.95, repetition_penalty=1.0,
|
153 |
):
|
154 |
temperature = float(0.9)
|
155 |
if temperature < 1e-2: temperature = 1e-2
|
@@ -163,18 +247,25 @@ def response(
|
|
163 |
seed=42,
|
164 |
)
|
165 |
|
166 |
-
|
|
|
|
|
167 |
if responsecritical(prompt)==False:
|
|
|
|
|
168 |
yield "Es scheint so, als sei dies keine Frage, die sich auf die Studienorientierung bezieht"
|
169 |
else:
|
170 |
answernc=parse_for_nc(prompt)
|
171 |
if answernc!="No":
|
|
|
|
|
172 |
yield answernc
|
173 |
else:
|
174 |
prompt = re.sub(r'\buni\b', 'Universität', prompt, flags=re.IGNORECASE)
|
175 |
addon=""
|
|
|
176 |
results=collection.query(
|
177 |
-
query_texts=[
|
178 |
n_results=60
|
179 |
)
|
180 |
|
@@ -185,13 +276,15 @@ def response(
|
|
185 |
if(len(results)>1):
|
186 |
addon=" Bitte berücksichtige bei deiner Antwort ausschießlich folgende Auszüge aus unserer Datenbank, sofern sie für die Antwort erforderlich sind. Beantworte die Frage knapp und präzise. Ignoriere unpassende Datenbank-Auszüge OHNE sie zu kommentieren, zu erwähnen oder aufzulisten:\n"+"\n".join(results)
|
187 |
system="Du bist ein deutschsprachiges KI-basiertes Studienberater Assistenzsystem, das zu jedem Anliegen möglichst geeignete Studieninformationen empfiehlt."+addon+"\n\nUser-Anliegen:"
|
188 |
-
formatted_prompt = format_prompt(system+"\n"+prompt)
|
189 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
190 |
output = ""
|
191 |
for response in stream:
|
192 |
output += response.token.text
|
193 |
osaanswer=simosa(prompt, osalist, osa)
|
194 |
output=output[:-4]+"\n"+osaanswer
|
|
|
|
|
195 |
yield output
|
196 |
|
197 |
gr.ChatInterface(response, chatbot=gr.Chatbot(value=[[None,"Herzlich willkommen! Ich bin Chätti ein KI-basiertes Studienassistenzsystem, das für jede Anfrage die besten Studieninformationen empfiehlt.<br>Erzähle mir was dich interessiert! Allgemein kann ich dir diesen Test (https://www.was-studiere-ich.de/) oder diese Seminare (https://www.bw-best.de) zur Studienfindung empfehlen."]],render_markdown=True),title="German Studyhelper Chätti").queue().launch(share=True) #False, server_name="0.0.0.0", server_port=7864)
|
|
|
3 |
import gradio as gr
|
4 |
import json
|
5 |
from huggingface_hub import InferenceClient
|
6 |
+
import gspread
|
7 |
+
from google.oauth2 import service_account
|
8 |
+
from datetime import datetime
|
9 |
+
import chromadb
|
10 |
+
|
11 |
+
# Google Sheets setup
|
12 |
+
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
13 |
+
key1 = os.getenv("key1")
|
14 |
+
key2 = os.getenv("key2")
|
15 |
+
key3 = os.getenv("key3")
|
16 |
+
key4 = os.getenv("key4")
|
17 |
+
key5 = os.getenv("key5")
|
18 |
+
key6 = os.getenv("key6")
|
19 |
+
key7 = os.getenv("key7")
|
20 |
+
key8 = os.getenv("key8")
|
21 |
+
key9 = os.getenv("key9")
|
22 |
+
key10 = os.getenv("key10")
|
23 |
+
key11 = os.getenv("key11")
|
24 |
+
key12 = os.getenv("key12")
|
25 |
+
key13 = os.getenv("key13")
|
26 |
+
key14 = os.getenv("key14")
|
27 |
+
key15 = os.getenv("key15")
|
28 |
+
key16 = os.getenv("key16")
|
29 |
+
key17 = os.getenv("key17")
|
30 |
+
key18 = os.getenv("key18")
|
31 |
+
key19 = os.getenv("key19")
|
32 |
+
key20 = os.getenv("key20")
|
33 |
+
key21 = os.getenv("key21")
|
34 |
+
key22 = os.getenv("key22")
|
35 |
+
key23 = os.getenv("key23")
|
36 |
+
key24 = os.getenv("key24")
|
37 |
+
key25 = os.getenv("key25")
|
38 |
+
key26 = os.getenv("key26")
|
39 |
+
key27 = os.getenv("key27")
|
40 |
+
key28 = os.getenv("key28")
|
41 |
+
pkey="-----BEGIN PRIVATE KEY-----\n"+key2+"\n"+key3+"\n"+ key4+"\n"+key5+"\n"+ key6+"\n"+key7+"\n"+key8+"\n"+key9+"\n"+key10+"\n"+key11+"\n"+key12+"\n"+key13+"\n"+key14+"\n"+key15+"\n"+key16+"\n"+key17+"\n"+key18+"\n"+key19+"\n"+key20+"\n"+key21+"\n"+key22+"\n"+key24+"\n"+key25+"\n"+key26+"\n"+key27+"\n"+key28+"\n-----END PRIVATE KEY-----\n"
|
42 |
+
json_data={
|
43 |
+
"type": "service_account",
|
44 |
+
"project_id": "nestolechatbot",
|
45 |
+
"private_key_id": key1,
|
46 |
+
"private_key": pkey,
|
47 |
+
"client_email": "[email protected]",
|
48 |
+
"client_email": "[email protected]",
|
49 |
+
"client_id": "107457262210035412036",
|
50 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
51 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
52 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
53 |
+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/nestoleservice%40nestolechatbot.iam.gserviceaccount.com",
|
54 |
+
"universe_domain": "googleapis.com"
|
55 |
+
}
|
56 |
+
creds = service_account.Credentials.from_service_account_info(json_data, scopes=scope)
|
57 |
+
|
58 |
+
client = gspread.authorize(creds)
|
59 |
+
sheet = client.open("nestolechatbot").sheet1 # Open the sheet
|
60 |
|
61 |
+
def save_to_sheet(date, name, message, IP, dev, header):
|
62 |
+
# Write user input to the Google Sheet
|
63 |
+
sheet.append_row([date, name, message, IP, dev, header])
|
64 |
+
return f"Thanks {name}, your message has been saved!"
|
65 |
+
|
66 |
path='/Users/thiloid/Desktop/LSKI/ole_nest/Chatbot/LLM/chroma'
|
67 |
if(os.path.exists(path)==False): path="/home/user/app/chroma"
|
68 |
|
|
|
168 |
return "No"
|
169 |
|
170 |
|
171 |
+
def extract_ip_and_device(headers_obj):
|
172 |
+
ip_address = None
|
173 |
+
device_info = None
|
174 |
+
|
175 |
+
# Access the raw headers list
|
176 |
+
headers = headers_obj.raw
|
177 |
+
|
178 |
+
for header in headers:
|
179 |
+
if len(header) != 2:
|
180 |
+
print(f"Unexpected header format: {header}")
|
181 |
+
continue
|
182 |
+
|
183 |
+
key, value = header
|
184 |
+
|
185 |
+
if key == b'x-forwarded-for':
|
186 |
+
ip_address = value.decode('utf-8')
|
187 |
+
elif key == b'user-agent':
|
188 |
+
device_info = value.decode('utf-8')
|
189 |
+
|
190 |
+
return ip_address, device_info
|
191 |
+
|
192 |
+
def format_prompt(message, history):
|
193 |
+
print("HISTORY")
|
194 |
+
print(history)
|
195 |
+
prompt = ""
|
196 |
+
if history:
|
197 |
+
user_prompt, bot_response = history[-1]
|
198 |
+
prompt += f"[INST] {user_prompt} [/INST] {bot_response}</s> "
|
199 |
+
prompt += f"[INST] {message} [/INST]"
|
200 |
+
print("Final P")
|
201 |
+
print(prompt)
|
202 |
+
return prompt
|
203 |
|
204 |
def responsecritical(
|
205 |
prompt, temperature=0.9, max_new_tokens=200, top_p=0.95, repetition_penalty=1.0,
|
|
|
233 |
|
234 |
|
235 |
def response(
|
236 |
+
request: gr.Request, prompt, temperature=0.9, max_new_tokens=500, top_p=0.95, repetition_penalty=1.0,
|
237 |
):
|
238 |
temperature = float(0.9)
|
239 |
if temperature < 1e-2: temperature = 1e-2
|
|
|
247 |
seed=42,
|
248 |
)
|
249 |
|
250 |
+
headers = request.headers
|
251 |
+
IP, dev = extract_ip_and_device(headers)
|
252 |
+
|
253 |
if responsecritical(prompt)==False:
|
254 |
+
now = str(datetime.now())
|
255 |
+
save_to_sheet(now, prompt, "Es scheint so, als sei dies keine Frage, die sich auf die Studienorientierung bezieht", IP, dev, str(headers))
|
256 |
yield "Es scheint so, als sei dies keine Frage, die sich auf die Studienorientierung bezieht"
|
257 |
else:
|
258 |
answernc=parse_for_nc(prompt)
|
259 |
if answernc!="No":
|
260 |
+
now = str(datetime.now())
|
261 |
+
save_to_sheet(now, prompt, answernc, IP, dev, str(headers))
|
262 |
yield answernc
|
263 |
else:
|
264 |
prompt = re.sub(r'\buni\b', 'Universität', prompt, flags=re.IGNORECASE)
|
265 |
addon=""
|
266 |
+
search_prompt = format_prompt(prompt, history)
|
267 |
results=collection.query(
|
268 |
+
query_texts=[search_prompt],
|
269 |
n_results=60
|
270 |
)
|
271 |
|
|
|
276 |
if(len(results)>1):
|
277 |
addon=" Bitte berücksichtige bei deiner Antwort ausschießlich folgende Auszüge aus unserer Datenbank, sofern sie für die Antwort erforderlich sind. Beantworte die Frage knapp und präzise. Ignoriere unpassende Datenbank-Auszüge OHNE sie zu kommentieren, zu erwähnen oder aufzulisten:\n"+"\n".join(results)
|
278 |
system="Du bist ein deutschsprachiges KI-basiertes Studienberater Assistenzsystem, das zu jedem Anliegen möglichst geeignete Studieninformationen empfiehlt."+addon+"\n\nUser-Anliegen:"
|
279 |
+
formatted_prompt = format_prompt(system+"\n"+prompt, history)
|
280 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
281 |
output = ""
|
282 |
for response in stream:
|
283 |
output += response.token.text
|
284 |
osaanswer=simosa(prompt, osalist, osa)
|
285 |
output=output[:-4]+"\n"+osaanswer
|
286 |
+
now = str(datetime.now())
|
287 |
+
save_to_sheet(now, prompt, output, IP, dev, str(headers))
|
288 |
yield output
|
289 |
|
290 |
gr.ChatInterface(response, chatbot=gr.Chatbot(value=[[None,"Herzlich willkommen! Ich bin Chätti ein KI-basiertes Studienassistenzsystem, das für jede Anfrage die besten Studieninformationen empfiehlt.<br>Erzähle mir was dich interessiert! Allgemein kann ich dir diesen Test (https://www.was-studiere-ich.de/) oder diese Seminare (https://www.bw-best.de) zur Studienfindung empfehlen."]],render_markdown=True),title="German Studyhelper Chätti").queue().launch(share=True) #False, server_name="0.0.0.0", server_port=7864)
|