Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import torch
|
|
6 |
import math
|
7 |
import httpcore
|
8 |
import pickle
|
|
|
9 |
setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
|
10 |
|
11 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
@@ -35,16 +36,19 @@ def respond(message,
|
|
35 |
queries = [
|
36 |
get_detailed_instruct(task, message)
|
37 |
]
|
|
|
|
|
38 |
|
39 |
query_embeddings = model.encode(queries, convert_to_tensor=True, normalize_embeddings=True)
|
40 |
scores = (query_embeddings @ encoded_questions.T) * 100
|
41 |
selected_references['similarity'] = scores.tolist()[0]
|
42 |
sorted_references = selected_references.sort_values(by='similarity', ascending=False)
|
43 |
-
print(sorted_references.shape[0])
|
44 |
sorted_references = sorted_references.iloc[:3]
|
45 |
sorted_references = sorted_references.sort_values(by='similarity', ascending=True)
|
46 |
print(sorted_references.shape[0])
|
47 |
print(sorted_references['similarity'].tolist())
|
|
|
|
|
48 |
|
49 |
from googletrans import Translator
|
50 |
translator = Translator()
|
@@ -59,21 +63,25 @@ def respond(message,
|
|
59 |
print(translated)
|
60 |
user = translated.text
|
61 |
print(user)
|
62 |
-
#print(row['assistant'])
|
63 |
assistant = translator.translate(row['assistant']).text
|
64 |
-
#print(assistant)
|
65 |
messages.append({"role": "user", "content":user })
|
66 |
messages.append({"role": "assistant", "content": assistant})
|
67 |
except Exception as error:
|
68 |
print("An error occurred:", error)
|
69 |
print("adding fatwa references exception occurred")
|
70 |
|
|
|
|
|
|
|
71 |
#adding more references
|
72 |
df = pd.read_csv("moslem-bot-reference.csv", sep='|')
|
73 |
for index, row in df.iterrows():
|
74 |
messages.append({"role": "user", "content": row['user']})
|
75 |
messages.append({"role": "assistant", "content": row['assistant']})
|
76 |
|
|
|
|
|
|
|
77 |
#history from chat session
|
78 |
"""
|
79 |
for val in history:
|
@@ -105,6 +113,8 @@ def respond(message,
|
|
105 |
print("en_message exception occurred")
|
106 |
|
107 |
#print(messages)
|
|
|
|
|
108 |
|
109 |
response = ""
|
110 |
|
@@ -119,6 +129,8 @@ def respond(message,
|
|
119 |
token = message.choices[0].delta.content
|
120 |
response += token
|
121 |
translated_response = translator.translate(response, src='en', dest=message_language).text
|
|
|
|
|
122 |
yield translated_response
|
123 |
except Exception as error:
|
124 |
print("An error occurred:", error)
|
|
|
6 |
import math
|
7 |
import httpcore
|
8 |
import pickle
|
9 |
+
import time
|
10 |
setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
|
11 |
|
12 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
|
|
36 |
queries = [
|
37 |
get_detailed_instruct(task, message)
|
38 |
]
|
39 |
+
print("start\n")
|
40 |
+
print(time.time())
|
41 |
|
42 |
query_embeddings = model.encode(queries, convert_to_tensor=True, normalize_embeddings=True)
|
43 |
scores = (query_embeddings @ encoded_questions.T) * 100
|
44 |
selected_references['similarity'] = scores.tolist()[0]
|
45 |
sorted_references = selected_references.sort_values(by='similarity', ascending=False)
|
|
|
46 |
sorted_references = sorted_references.iloc[:3]
|
47 |
sorted_references = sorted_references.sort_values(by='similarity', ascending=True)
|
48 |
print(sorted_references.shape[0])
|
49 |
print(sorted_references['similarity'].tolist())
|
50 |
+
print("sorted references\n")
|
51 |
+
print(time.time())
|
52 |
|
53 |
from googletrans import Translator
|
54 |
translator = Translator()
|
|
|
63 |
print(translated)
|
64 |
user = translated.text
|
65 |
print(user)
|
|
|
66 |
assistant = translator.translate(row['assistant']).text
|
|
|
67 |
messages.append({"role": "user", "content":user })
|
68 |
messages.append({"role": "assistant", "content": assistant})
|
69 |
except Exception as error:
|
70 |
print("An error occurred:", error)
|
71 |
print("adding fatwa references exception occurred")
|
72 |
|
73 |
+
print("append references\n")
|
74 |
+
print(time.time())
|
75 |
+
|
76 |
#adding more references
|
77 |
df = pd.read_csv("moslem-bot-reference.csv", sep='|')
|
78 |
for index, row in df.iterrows():
|
79 |
messages.append({"role": "user", "content": row['user']})
|
80 |
messages.append({"role": "assistant", "content": row['assistant']})
|
81 |
|
82 |
+
print("added more references\n")
|
83 |
+
print(time.time())
|
84 |
+
|
85 |
#history from chat session
|
86 |
"""
|
87 |
for val in history:
|
|
|
113 |
print("en_message exception occurred")
|
114 |
|
115 |
#print(messages)
|
116 |
+
print("added last question\n")
|
117 |
+
print(time.time())
|
118 |
|
119 |
response = ""
|
120 |
|
|
|
129 |
token = message.choices[0].delta.content
|
130 |
response += token
|
131 |
translated_response = translator.translate(response, src='en', dest=message_language).text
|
132 |
+
print("yield response\n")
|
133 |
+
print(time.time())
|
134 |
yield translated_response
|
135 |
except Exception as error:
|
136 |
print("An error occurred:", error)
|