AminFaraji commited on
Commit
db1f2ae
·
verified ·
1 Parent(s): b92c047

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +181 -7
app.py CHANGED
@@ -1,9 +1,183 @@
1
- import gradio as gr
2
- import langchain
3
- import google
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- def greet(name):
6
- return "Helloooooo " + name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
9
- demo.launch()
 
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 = "/content/drive/My Drive/chroma8"
51
+ # call the chroma generated in a directory
52
+ db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embeddings)
53
+
54
+
55
+ from transformers import AutoTokenizer
56
+ import transformers
57
+ import torch
58
+
59
+ model = "tiiuae/falcon-7b-instruct" # meta-llama/Llama-2-7b-chat-hf
60
+
61
+ tokenizer = AutoTokenizer.from_pretrained(model, use_auth_token=True)
62
+
63
+
64
+
65
+
66
+ from transformers import pipeline
67
+
68
+ llama_pipeline = pipeline(
69
+ "text-generation", # LLM task
70
+ model=model,
71
+ torch_dtype=torch.float16,
72
+ device_map="auto",
73
+ )
74
+
75
+
76
+
77
+ def get_response(prompt: str) -> None:
78
+ """
79
+ Generate a response from the Llama model.
80
+
81
+ Parameters:
82
+ prompt (str): The user's input/question for the model.
83
+
84
+ Returns:
85
+ None: Prints the model's response.
86
+ """
87
+ sequences = llama_pipeline(
88
+ prompt,
89
+ do_sample=True,
90
+ top_k=10,
91
+ num_return_sequences=1,
92
+ eos_token_id=tokenizer.eos_token_id,
93
+ max_length=256,
94
+ )
95
+ print("Chatbot:", sequences[0]['generated_text'])
96
+
97
+
98
+
99
+
100
+ 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"
101
 
102
+ Context:
103
+ {context}
104
+
105
+ {query}""".strip()
106
+
107
+ from langchain.prompts import PromptTemplate
108
+
109
+ prompt_template = PromptTemplate(
110
+ input_variables=["query", "context"],
111
+ template=template
112
+ )
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+ # Generate a response from the Llama model
125
+ def get_llama_response(message: str, history: list) -> str:
126
+ """
127
+ Generates a conversational response from the Llama model.
128
+
129
+ Parameters:
130
+ message (str): User's input message.
131
+ history (list): Past conversation history.
132
+
133
+ Returns:
134
+ str: Generated response from the Llama model.
135
+ """
136
+ print('messageeeeeeeeeeeeeee:',message)
137
+ #query = format_message(message, history)
138
+ response = ""
139
+
140
+ query = """
141
+ Answer the question based only on the following context. Dont provide any information out of the context:
142
+
143
+ {context}
144
+
145
+ ---
146
+
147
+ Answer the question based on the above context: {question}
148
+ """
149
+
150
+ #message='how does alice meet the mad hatter?'
151
+ ######################
152
+ # Search the DB for similar documents to the query.
153
+ results = db.similarity_search_with_relevance_scores(message, k=3)
154
+ if len(results) == 0 or results[0][1] < 0.5:
155
+ print(f"Unable to find matching results.")
156
+
157
+
158
+ context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results])
159
+ #context_text="amin is a math student."
160
+ ####################3
161
+
162
+ query = prompt_template.format(query=message, context=context_text)
163
+ #query=query.format(context=context_text,question=message)
164
+ sequences = llama_pipeline(
165
+ query,
166
+ do_sample=True,
167
+ top_k=10,
168
+ num_return_sequences=1,
169
+ eos_token_id=tokenizer.eos_token_id,
170
+ max_length=1024,
171
+ )
172
+
173
+ generated_text = sequences[0]['generated_text']
174
+ response = generated_text[len(query):] # Remove the prompt from the output
175
+
176
+ print("Chatbot:", response.strip())
177
+ return response.strip()
178
+
179
+
180
+
181
+ import gradio as gr
182
 
183
+ gr.ChatInterface(get_llama_response).launch()