Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,196 +0,0 @@
|
|
1 |
-
import argparse
|
2 |
-
# from dataclasses import dataclass
|
3 |
-
from langchain_community.vectorstores import Chroma
|
4 |
-
#from langchain_openai import OpenAIEmbeddings
|
5 |
-
#from langchain_openai import ChatOpenAI
|
6 |
-
from langchain.prompts import ChatPromptTemplate
|
7 |
-
|
8 |
-
|
9 |
-
# a template by which the bot will answer the quetion according the "context" of
|
10 |
-
# the text that will be imported as context later, determines the information that the question should be answered according to.
|
11 |
-
PROMPT_TEMPLATE = """
|
12 |
-
Answer the question based only on the following context:
|
13 |
-
|
14 |
-
{context}
|
15 |
-
|
16 |
-
---
|
17 |
-
|
18 |
-
Answer the question based on the above context: {question}
|
19 |
-
"""
|
20 |
-
# from langchain.document_loaders import DirectoryLoader
|
21 |
-
from langchain_community.document_loaders import DirectoryLoader
|
22 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
23 |
-
from langchain.schema import Document
|
24 |
-
# from langchain.embeddings import OpenAIEmbeddings
|
25 |
-
#from langchain_openai import OpenAIEmbeddings
|
26 |
-
from langchain_community.vectorstores import Chroma
|
27 |
-
import openai
|
28 |
-
from dotenv import load_dotenv
|
29 |
-
import os
|
30 |
-
import shutil
|
31 |
-
# a custom embedding
|
32 |
-
from sentence_transformers import SentenceTransformer
|
33 |
-
from langchain_experimental.text_splitter import SemanticChunker
|
34 |
-
from typing import List
|
35 |
-
|
36 |
-
|
37 |
-
class MyEmbeddings:
|
38 |
-
def __init__(self):
|
39 |
-
self.model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
40 |
-
|
41 |
-
def embed_documents(self, texts: List[str]) -> List[List[float]]:
|
42 |
-
return [self.model.encode(t).tolist() for t in texts]
|
43 |
-
def embed_query(self, query: str) -> List[float]:
|
44 |
-
return [self.model.encode([query])][0][0].tolist()
|
45 |
-
|
46 |
-
|
47 |
-
embeddings = MyEmbeddings()
|
48 |
-
|
49 |
-
splitter = SemanticChunker(embeddings)
|
50 |
-
CHROMA_PATH = "chroma8"
|
51 |
-
# call the chroma generated in a directory
|
52 |
-
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embeddings)
|
53 |
-
|
54 |
-
|
55 |
-
import os
|
56 |
-
from huggingface_hub import login
|
57 |
-
|
58 |
-
# Retrieve the token from the environment variable
|
59 |
-
token = os.getenv('HF_Token')
|
60 |
-
|
61 |
-
# Log in using the token
|
62 |
-
login(token=token)
|
63 |
-
from transformers import AutoTokenizer
|
64 |
-
import transformers
|
65 |
-
import torch
|
66 |
-
|
67 |
-
model = "tiiuae/falcon-7b-instruct" # meta-llama/Llama-2-7b-chat-hf
|
68 |
-
|
69 |
-
tokenizer = AutoTokenizer.from_pretrained(model, use_auth_token=True)
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
from transformers import pipeline
|
75 |
-
|
76 |
-
llama_pipeline = pipeline(
|
77 |
-
"text-generation", # LLM task
|
78 |
-
model=model,
|
79 |
-
torch_dtype=torch.float16,
|
80 |
-
device_map="auto",
|
81 |
-
)
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
def get_response(prompt: str) -> None:
|
86 |
-
"""
|
87 |
-
Generate a response from the Llama model.
|
88 |
-
|
89 |
-
Parameters:
|
90 |
-
prompt (str): The user's input/question for the model.
|
91 |
-
|
92 |
-
Returns:
|
93 |
-
None: Prints the model's response.
|
94 |
-
"""
|
95 |
-
sequences = llama_pipeline(
|
96 |
-
prompt,
|
97 |
-
do_sample=True,
|
98 |
-
top_k=10,
|
99 |
-
num_return_sequences=1,
|
100 |
-
eos_token_id=tokenizer.eos_token_id,
|
101 |
-
max_length=256,
|
102 |
-
)
|
103 |
-
print("Chatbot:", sequences[0]['generated_text'])
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
template = """Answer the query based only the provided context, and if the answer is not contained within the context below, say "I don't knowwwww"
|
109 |
-
|
110 |
-
Context:
|
111 |
-
{context}
|
112 |
-
|
113 |
-
{query}""".strip()
|
114 |
-
|
115 |
-
from langchain.prompts import PromptTemplate
|
116 |
-
|
117 |
-
prompt_template = PromptTemplate(
|
118 |
-
input_variables=["query", "context"],
|
119 |
-
template=template
|
120 |
-
)
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
# Generate a response from the Llama model
|
133 |
-
def get_llama_response(message: str, history: list) -> str:
|
134 |
-
"""
|
135 |
-
Generates a conversational response from the Llama model.
|
136 |
-
|
137 |
-
Parameters:
|
138 |
-
message (str): User's input message.
|
139 |
-
history (list): Past conversation history.
|
140 |
-
|
141 |
-
Returns:
|
142 |
-
str: Generated response from the Llama model.
|
143 |
-
"""
|
144 |
-
print('messageeeeeeeeeeeeeee:',message)
|
145 |
-
#query = format_message(message, history)
|
146 |
-
response = ""
|
147 |
-
|
148 |
-
query = """
|
149 |
-
Answer the question based only on the following context. Dont provide any information out of the context:
|
150 |
-
|
151 |
-
{context}
|
152 |
-
|
153 |
-
---
|
154 |
-
|
155 |
-
Answer the question based on the above context: {question}
|
156 |
-
"""
|
157 |
-
|
158 |
-
#message='how does alice meet the mad hatter?'
|
159 |
-
######################
|
160 |
-
# Search the DB for similar documents to the query.
|
161 |
-
print('before searching inside the db')
|
162 |
-
results = db.similarity_search_with_relevance_scores(message, k=3)
|
163 |
-
if len(results) == 0 or results[0][1] < 0.5:
|
164 |
-
print(f"Unable to find matching results.")
|
165 |
-
print('after searchingf insidee the db')
|
166 |
-
|
167 |
-
|
168 |
-
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
|
169 |
-
#context_text="amin is a math student."
|
170 |
-
####################3
|
171 |
-
|
172 |
-
query = prompt_template.format(query=message, context=context_text)
|
173 |
-
#query=query.format(context=context_text,question=message)
|
174 |
-
print('im gonna generate response')
|
175 |
-
sequences = llama_pipeline(
|
176 |
-
query,
|
177 |
-
do_sample=True,
|
178 |
-
top_k=10,
|
179 |
-
num_return_sequences=1,
|
180 |
-
eos_token_id=tokenizer.eos_token_id,
|
181 |
-
max_length=1024,
|
182 |
-
)
|
183 |
-
|
184 |
-
print('igenerated response')
|
185 |
-
generated_text = sequences[0]['generated_text']
|
186 |
-
response = generated_text[len(query):] # Remove the prompt from the output
|
187 |
-
|
188 |
-
print("Chatbot:", response.strip())
|
189 |
-
print('i wanneea return')
|
190 |
-
return response.strip()
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
import gradio as gr
|
195 |
-
|
196 |
-
gr.ChatInterface(get_llama_response).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|