Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,35 +9,89 @@ import pickle
|
|
9 |
import time
|
10 |
setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
|
11 |
|
|
|
|
|
|
|
12 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
13 |
model = SentenceTransformer('intfloat/multilingual-e5-large-instruct')
|
14 |
|
15 |
def get_detailed_instruct(task_description: str, query: str) -> str:
|
16 |
-
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
25 |
|
26 |
#make a moslem bot
|
27 |
messages.append({"role": "user", "content": "I want you to answer strictly based on quran and hadith"})
|
28 |
messages.append({"role": "assistant", "content": "I'd be happy to help! Please go ahead and provide the sentence you'd like me to analyze. Please specify whether you're referencing a particular verse or hadith (Prophetic tradition) from the Quran or Hadith, or if you're asking me to analyze a general statement."})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
#adding more references
|
31 |
df = pd.read_csv("moslem-bot-reference.csv", sep='|')
|
32 |
for index, row in df.iterrows():
|
33 |
messages.append({"role": "user", "content": row['user']})
|
34 |
messages.append({"role": "assistant", "content": row['assistant']})
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
#latest user question
|
40 |
-
from googletrans import Translator
|
41 |
translator = Translator()
|
42 |
en_message = ""
|
43 |
message_language = "en"
|
@@ -56,13 +110,9 @@ def respond(message,
|
|
56 |
messages.append({"role": "user", "content": message})
|
57 |
print("An error occurred:", error)
|
58 |
print("en_message exception occurred")
|
59 |
-
|
60 |
-
|
61 |
-
print(messages)
|
62 |
-
print("added last question\n")
|
63 |
-
print(time.time())
|
64 |
|
65 |
response = ""
|
|
|
66 |
for message in client.chat_completion(
|
67 |
messages,
|
68 |
max_tokens=max_tokens,
|
@@ -73,46 +123,22 @@ def respond(message,
|
|
73 |
token = message.choices[0].delta.content
|
74 |
|
75 |
response += token
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
stream=True,
|
82 |
-
temperature=temperature,
|
83 |
-
top_p=top_p,
|
84 |
-
):
|
85 |
-
try:
|
86 |
-
print("cek1")
|
87 |
-
if(message):
|
88 |
-
print("cek2")
|
89 |
-
if len(message.choices)>0:
|
90 |
-
print("cek3")
|
91 |
-
token = message.choices[0].delta.content
|
92 |
-
response += token
|
93 |
-
if(len(response)>0):
|
94 |
-
print("cek4")
|
95 |
-
translated = translator.translate(response, src='en', dest=message_language)
|
96 |
-
if not (translated is None):
|
97 |
-
print("cek5")
|
98 |
-
translated_response = translated.text
|
99 |
-
yield translated_response
|
100 |
-
else:
|
101 |
-
yield response
|
102 |
-
else:
|
103 |
-
yield response
|
104 |
-
else:
|
105 |
-
yield response
|
106 |
-
else:
|
107 |
-
yield response
|
108 |
-
except Exception as error:
|
109 |
-
print("An error occurred:", error)
|
110 |
yield response
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
114 |
additional_inputs=[
|
115 |
-
gr.
|
|
|
116 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
117 |
gr.Slider(
|
118 |
minimum=0.1,
|
@@ -121,16 +147,9 @@ demo = gr.Interface(
|
|
121 |
step=0.05,
|
122 |
label="Top-p (nucleus sampling)",
|
123 |
),
|
124 |
-
],
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
examples=[
|
129 |
-
["Why is men created?"],
|
130 |
-
["Please tell me about superstition!"],
|
131 |
-
["How moses defeat pharaoh?"],
|
132 |
-
],
|
133 |
-
title="Moslem Bot")
|
134 |
-
|
135 |
if __name__ == "__main__":
|
136 |
-
demo.launch()
|
|
|
9 |
import time
|
10 |
setattr(httpcore, 'SyncHTTPTransport', 'AsyncHTTPProxy')
|
11 |
|
12 |
+
"""
|
13 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
14 |
+
"""
|
15 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
16 |
model = SentenceTransformer('intfloat/multilingual-e5-large-instruct')
|
17 |
|
18 |
def get_detailed_instruct(task_description: str, query: str) -> str:
|
19 |
+
return f'Instruct: {task_description}\nQuery: {query}'
|
20 |
|
21 |
+
|
22 |
+
def respond(
|
23 |
+
message,
|
24 |
+
history: list[tuple[str, str]],
|
25 |
+
system_message,
|
26 |
+
max_tokens,
|
27 |
+
temperature,
|
28 |
+
top_p,
|
29 |
+
):
|
30 |
+
messages = [{"role": "system", "content": system_message}]
|
31 |
|
32 |
#make a moslem bot
|
33 |
messages.append({"role": "user", "content": "I want you to answer strictly based on quran and hadith"})
|
34 |
messages.append({"role": "assistant", "content": "I'd be happy to help! Please go ahead and provide the sentence you'd like me to analyze. Please specify whether you're referencing a particular verse or hadith (Prophetic tradition) from the Quran or Hadith, or if you're asking me to analyze a general statement."})
|
35 |
+
|
36 |
+
#adding fatwa references
|
37 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
38 |
+
selected_references = torch.load('selected_references.sav', map_location=torch.device(device))
|
39 |
+
encoded_questions = torch.load('encoded_questions.sav', map_location=torch.device(device))
|
40 |
+
|
41 |
+
task = 'Given a web search query, retrieve relevant passages that answer the query'
|
42 |
+
queries = [
|
43 |
+
get_detailed_instruct(task, message)
|
44 |
+
]
|
45 |
+
print("start\n")
|
46 |
+
print(time.time())
|
47 |
+
|
48 |
+
query_embeddings = model.encode(queries, convert_to_tensor=True, normalize_embeddings=True)
|
49 |
+
scores = (query_embeddings @ encoded_questions.T) * 100
|
50 |
+
selected_references['similarity'] = scores.tolist()[0]
|
51 |
+
sorted_references = selected_references.sort_values(by='similarity', ascending=False)
|
52 |
+
sorted_references = sorted_references.iloc[:1]
|
53 |
+
sorted_references = sorted_references.sort_values(by='similarity', ascending=True)
|
54 |
+
print(sorted_references.shape[0])
|
55 |
+
print(sorted_references['similarity'].tolist())
|
56 |
+
print("sorted references\n")
|
57 |
+
print(time.time())
|
58 |
+
|
59 |
+
from googletrans import Translator
|
60 |
+
translator = Translator()
|
61 |
+
|
62 |
+
for index, row in sorted_references.iterrows():
|
63 |
+
if(type(row["user"]) is str and type(row['assistant']) is str):
|
64 |
+
try:
|
65 |
+
translator = Translator()
|
66 |
+
print(index)
|
67 |
+
print(f'{row["user"]}')
|
68 |
+
translated = translator.translate(f'{row["user"]}', src='ar', dest='en')
|
69 |
+
print(translated)
|
70 |
+
user = translated.text
|
71 |
+
print(user)
|
72 |
+
assistant = translator.translate(row['assistant']).text
|
73 |
+
messages.append({"role": "user", "content":user })
|
74 |
+
messages.append({"role": "assistant", "content": assistant})
|
75 |
+
except Exception as error:
|
76 |
+
print("1. An error occurred:", error)
|
77 |
+
print("adding fatwa references exception occurred")
|
78 |
+
|
79 |
+
print("append references\n")
|
80 |
+
print(time.time())
|
81 |
|
82 |
#adding more references
|
83 |
df = pd.read_csv("moslem-bot-reference.csv", sep='|')
|
84 |
for index, row in df.iterrows():
|
85 |
messages.append({"role": "user", "content": row['user']})
|
86 |
messages.append({"role": "assistant", "content": row['assistant']})
|
87 |
+
|
88 |
+
for val in history:
|
89 |
+
if val[0]:
|
90 |
+
messages.append({"role": "user", "content": val[0]})
|
91 |
+
if val[1]:
|
92 |
+
messages.append({"role": "assistant", "content": val[1]})
|
93 |
|
94 |
#latest user question
|
|
|
95 |
translator = Translator()
|
96 |
en_message = ""
|
97 |
message_language = "en"
|
|
|
110 |
messages.append({"role": "user", "content": message})
|
111 |
print("An error occurred:", error)
|
112 |
print("en_message exception occurred")
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
response = ""
|
115 |
+
|
116 |
for message in client.chat_completion(
|
117 |
messages,
|
118 |
max_tokens=max_tokens,
|
|
|
123 |
token = message.choices[0].delta.content
|
124 |
|
125 |
response += token
|
126 |
+
|
127 |
+
translated_response = translator.translate(response, src='en', dest=message_language).text
|
128 |
+
if(len(translated_response)>0):
|
129 |
+
yield translated_response
|
130 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
yield response
|
132 |
+
|
133 |
+
|
134 |
+
"""
|
135 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
136 |
+
"""
|
137 |
+
demo = gr.ChatInterface(
|
138 |
+
respond,
|
139 |
additional_inputs=[
|
140 |
+
gr.Textbox(value="You are a sunni moslem bot that always give answer based on quran, hadith, and the companions of prophet Muhammad!", label="System message"),
|
141 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
142 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
143 |
gr.Slider(
|
144 |
minimum=0.1,
|
|
|
147 |
step=0.05,
|
148 |
label="Top-p (nucleus sampling)",
|
149 |
),
|
150 |
+
],
|
151 |
+
)
|
152 |
+
|
153 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
if __name__ == "__main__":
|
155 |
+
demo.launch()
|