Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,52 +26,6 @@ def respond(message,
|
|
| 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 fatwa references
|
| 31 |
-
'''device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 32 |
-
selected_references = torch.load('selected_references.sav', map_location=torch.device(device))
|
| 33 |
-
encoded_questions = torch.load('encoded_questions.sav', map_location=torch.device(device))
|
| 34 |
-
|
| 35 |
-
task = 'Given a web search query, retrieve relevant passages that answer the query'
|
| 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[:1]
|
| 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()
|
| 55 |
-
|
| 56 |
-
for index, row in sorted_references.iterrows():
|
| 57 |
-
if(type(row["user"]) is str and type(row['assistant']) is str):
|
| 58 |
-
try:
|
| 59 |
-
translator = Translator()
|
| 60 |
-
print(index)
|
| 61 |
-
print(f'{row["user"]}')
|
| 62 |
-
translated = translator.translate(f'{row["user"]}', src='ar', dest='en')
|
| 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='|')
|
|
@@ -112,12 +66,24 @@ def respond(message,
|
|
| 112 |
print("An error occurred:", error)
|
| 113 |
print("en_message exception occurred")
|
| 114 |
|
|
|
|
| 115 |
print(messages)
|
| 116 |
print("added last question\n")
|
| 117 |
print(time.time())
|
| 118 |
|
| 119 |
response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
|
|
|
|
|
|
|
|
|
| 121 |
for message in client.chat_completion(
|
| 122 |
messages,
|
| 123 |
max_tokens=max_tokens,
|
|
@@ -151,7 +117,7 @@ def respond(message,
|
|
| 151 |
except Exception as error:
|
| 152 |
print("An error occurred:", error)
|
| 153 |
yield response
|
| 154 |
-
|
| 155 |
demo = gr.Interface(
|
| 156 |
fn=respond,
|
| 157 |
additional_inputs=[
|
|
|
|
| 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='|')
|
|
|
|
| 66 |
print("An error occurred:", error)
|
| 67 |
print("en_message exception occurred")
|
| 68 |
|
| 69 |
+
|
| 70 |
print(messages)
|
| 71 |
print("added last question\n")
|
| 72 |
print(time.time())
|
| 73 |
|
| 74 |
response = ""
|
| 75 |
+
for message in client.chat_completion(
|
| 76 |
+
messages,
|
| 77 |
+
max_tokens=max_tokens,
|
| 78 |
+
stream=True,
|
| 79 |
+
temperature=temperature,
|
| 80 |
+
top_p=top_p,
|
| 81 |
+
):
|
| 82 |
+
token = message.choices[0].delta.content
|
| 83 |
|
| 84 |
+
response += token
|
| 85 |
+
yield response
|
| 86 |
+
"""
|
| 87 |
for message in client.chat_completion(
|
| 88 |
messages,
|
| 89 |
max_tokens=max_tokens,
|
|
|
|
| 117 |
except Exception as error:
|
| 118 |
print("An error occurred:", error)
|
| 119 |
yield response
|
| 120 |
+
"""
|
| 121 |
demo = gr.Interface(
|
| 122 |
fn=respond,
|
| 123 |
additional_inputs=[
|