Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import gradio as gr
|
4 |
+
import qdrant_client
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
+
from langchain.vectorstores import Qdrant
|
8 |
+
from langchain_openai import ChatOpenAI
|
9 |
+
|
10 |
+
|
11 |
+
_ = load_dotenv()
|
12 |
+
|
13 |
+
QDRANT_URL= os.getenv('QDRANT_URL')
|
14 |
+
QDRANT_API_KEY= os.getenv('QDRANT_API_KEY')
|
15 |
+
OPEN_AI_TOKEN= os.getenv('OPEN_AI_TOKEN')
|
16 |
+
|
17 |
+
|
18 |
+
def clean_text(text):
|
19 |
+
text = re.sub(r'<[^>]*>', '', text)
|
20 |
+
text = re.sub(r'[^\w\s]', '', text)
|
21 |
+
text = re.sub(r'\s+', ' ', text)
|
22 |
+
return text.lower().strip()
|
23 |
+
|
24 |
+
|
25 |
+
collection_name = "Rag-with-Langchain-qdrant-Hadith"
|
26 |
+
|
27 |
+
client = qdrant_client.QdrantClient(
|
28 |
+
url=QDRANT_URL,
|
29 |
+
api_key=QDRANT_API_KEY
|
30 |
+
)
|
31 |
+
|
32 |
+
collection_config = qdrant_client.http.models.VectorParams(
|
33 |
+
size = 384,
|
34 |
+
distance = qdrant_client.http.models.Distance.COSINE
|
35 |
+
)
|
36 |
+
|
37 |
+
embeddings = HuggingFaceEmbeddings(
|
38 |
+
model_name = "intfloat/multilingual-e5-small"
|
39 |
+
)
|
40 |
+
|
41 |
+
vectorStore = Qdrant(
|
42 |
+
client = client,
|
43 |
+
collection_name = collection_name,
|
44 |
+
embeddings = embeddings
|
45 |
+
)
|
46 |
+
|
47 |
+
|
48 |
+
def get_relevant_docs(question,k):
|
49 |
+
relevant_docs = vectorStore.similarity_search_with_score(query=question,k=k)
|
50 |
+
return relevant_docs
|
51 |
+
|
52 |
+
def extract_contexts(relevant_docs):
|
53 |
+
contexts = []
|
54 |
+
for doc in relevant_docs:
|
55 |
+
contexts.append(doc[0].page_content)
|
56 |
+
return contexts
|
57 |
+
|
58 |
+
def create_template(question,k):
|
59 |
+
relevant_docs = get_relevant_docs(question,k)
|
60 |
+
contexts = extract_contexts(relevant_docs)
|
61 |
+
template = f"""
|
62 |
+
Engage in a conversation with the user, responding to their question:
|
63 |
+
{question}
|
64 |
+
within this contexts of Hadiths:
|
65 |
+
{contexts}
|
66 |
+
Encourage the model to provide informative and culturally sensitive answers, reflecting Islamic teachings. Maintain a conversational tone and aim for clarity in responses and make sure they are restricted extracted from the provided contexts and i want you to answer me in arabic."""
|
67 |
+
return template
|
68 |
+
|
69 |
+
|
70 |
+
chat = ChatOpenAI(openai_api_key=OPEN_AI_TOKEN, model='gpt-3.5-turbo', temperature=0.5)
|
71 |
+
|
72 |
+
def generate_answer(question):
|
73 |
+
cleaned_question= clean_text(question)
|
74 |
+
query= create_template(cleaned_question,10)
|
75 |
+
response = clean_text(chat.invoke(query).content)
|
76 |
+
return response
|
77 |
+
|
78 |
+
def greet(question):
|
79 |
+
answer= generate_answer(question)
|
80 |
+
return answer
|
81 |
+
|
82 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
83 |
+
iface.launch(inline = False)
|