AndreaAlessandrelli4 commited on
Commit
12a00c8
·
verified ·
1 Parent(s): 2706320

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -2
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import os
2
  from threading import Thread
3
  from typing import Iterator
4
-
 
 
5
  import gradio as gr
6
  import spaces
7
  import torch
@@ -22,6 +24,52 @@ if torch.cuda.is_available():
22
  model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", load_in_4bit=True)
23
  tokenizer = AutoTokenizer.from_pretrained(model_id)
24
  tokenizer.use_default_system_prompt = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
 
27
  @spaces.GPU
@@ -44,7 +92,19 @@ def generate(
44
  Firmati alla fine di ogni risposta '-AvvoChat'.'''})
45
  for user, assistant in chat_history:
46
  conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
47
- conversation.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
50
  if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
 
1
  import os
2
  from threading import Thread
3
  from typing import Iterator
4
+ from haystack import Document
5
+ from haystack import Pipeline
6
+ from haystack.components.embedders import SentenceTransformersTextEmbedder
7
  import gradio as gr
8
  import spaces
9
  import torch
 
24
  model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", load_in_4bit=True)
25
  tokenizer = AutoTokenizer.from_pretrained(model_id)
26
  tokenizer.use_default_system_prompt = False
27
+ query_pipeline1 = Pipeline()
28
+ query_pipeline1.add_component(
29
+ "text_embedder",
30
+ SentenceTransformersTextEmbedder(
31
+ model="intfloat/multilingual-e5-large",
32
+ prefix="query:",
33
+ ))
34
+
35
+
36
+
37
+
38
+ key='4vNfIDO8PmFwCloxA40y2b4PSHm62vmcuPoM'
39
+ url = "https://mmchpi0yssanukk5t3ofta.c0.europe-west3.gcp.weaviate.cloud"
40
+ # instanziamento client weaviate
41
+ client = weaviate.Client(
42
+ url = url,
43
+ auth_client_secret=weaviate.auth.AuthApiKey(api_key=key),
44
+ #embedded_options=weaviate.embedded.EmbeddedOptions(),
45
+ )
46
+
47
+
48
+
49
+
50
+
51
+ def vettorizz_query(query):
52
+ vector_query=query_pipeline1.run({ "text_embedder": {"text": query},
53
+ })['text_embedder']['embedding']
54
+ return query, vector_query
55
+
56
+ def richiamo_materiali(query, alpha, N_items):
57
+ text_query, vett_query = vettorizz_query(query)
58
+ try:
59
+ materiali = client.query.get("Default", ["content"]).with_hybrid(
60
+ query=text_query,
61
+ vector=vett_query,
62
+ alpha=alpha,
63
+ fusion_type=HybridFusion.RELATIVE_SCORE,
64
+ ).with_additional(["score"]).with_limit(N_items).do()
65
+
66
+ mat = [{'score':i['_additional']['score'],'content':i['content']} for i in materiali['data']['Get']['Default']]
67
+ except:
68
+ mat =[{'score':0, 'content':'NESSUN MATERIALE FORNITO'}]
69
+
70
+ return mat
71
+
72
+
73
 
74
 
75
  @spaces.GPU
 
92
  Firmati alla fine di ogni risposta '-AvvoChat'.'''})
93
  for user, assistant in chat_history:
94
  conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
95
+ materiali = richiamo_materiali(message, alpha=1.0, n_items=5)
96
+ documenti = ''
97
+ for idx, d in enumerate(materiali):
98
+ if idx<len(materiali)-1:
99
+ documenti += f"{d['content']}; "
100
+ else:
101
+ documenti += f"{d['content']}. "
102
+ text = f'''Basandoti sulle tue conoscenze e usando le informazioni contenute che ti fornisco di seguito.
103
+ CONTESTO:
104
+ {documenti}
105
+ Rispondi in modo esaustivo, evitando inutili giri di parole o ripetizioni, alla seguente domanda.
106
+ {message}'''
107
+ conversation.append({"role": "user", "content": text})
108
 
109
  input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt")
110
  if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH: