Can Günen commited on
Commit
fa728c7
·
1 Parent(s): a168862

reduced the methods in class and add a load config buttion

Browse files
Files changed (2) hide show
  1. app.py +5 -4
  2. document_chatbot.py +13 -9
app.py CHANGED
@@ -13,10 +13,11 @@ with gr.Blocks() as demo:
13
  text_input = gr.Textbox(label="Enter text or URL to text file")
14
  with gr.Column():
15
  with gr.Row():
16
- api_key_input = gr.Textbox(label="Enter HF Token to load the model")
17
- api_key_input.submit(document_chatbot.load_token, inputs=api_key_input, outputs=api_key_input)
18
- picked_model = gr.Dropdown(["google/flan-t5-large", "google/flan-t5-base","google/flan-t5-small"], label="Models")
19
- picked_model.change(document_chatbot.load_model, picked_model)
 
20
  chatbot = gr.Chatbot()
21
 
22
  q_input = gr.Textbox(label="Please write your question")
 
13
  text_input = gr.Textbox(label="Enter text or URL to text file")
14
  with gr.Column():
15
  with gr.Row():
16
+ api_key_input = gr.Textbox(label="Enter HF Token")
17
+ picked_model = gr.Dropdown(["google/flan-t5-large", "google/flan-t5-base","google/flan-t5-small"], label="Models", interactive=True)
18
+ load_configs = gr.Button("Load configs")
19
+ load_configs.click(document_chatbot.load_token_and_model, inputs=[api_key_input, picked_model], outputs=[api_key_input])
20
+
21
  chatbot = gr.Chatbot()
22
 
23
  q_input = gr.Textbox(label="Please write your question")
document_chatbot.py CHANGED
@@ -12,6 +12,9 @@ from langchain.vectorstores import FAISS
12
  from langchain import HuggingFaceHub
13
 
14
 
 
 
 
15
  class DocumentChatbot:
16
 
17
  def __init__(self):
@@ -21,23 +24,24 @@ class DocumentChatbot:
21
  self.metadata = {"source": "internet"}
22
  self.init_mes = ["According to the document, ", "Based on the text, ", "I think, ", "According to the text, ", "Based on the document you provided, "]
23
 
24
- def load_token(self, api_key):
 
 
25
  if api_key[:2] == "hf":
26
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = api_key
27
  result = subprocess.run(["curl", "https://huggingface.co/api/whoami-v2", "-H", f"Authorization: Bearer {api_key}"], capture_output=True).stdout.decode()
28
  if result == '{"error":"Invalid username or password."}':
29
  return "Invalid API token"
30
  else:
31
- return "HF Token successfully registered"
 
 
 
32
 
33
- def load_model(self, model_name):
34
- self.llm = HuggingFaceHub(repo_id=model_name, model_kwargs={"temperature":0, "max_length":512})
35
- self.chain = load_qa_chain(self.llm, chain_type="stuff")
36
- self.embeddings = HuggingFaceEmbeddings()
37
- return f"Successfully loaded {model_name}"
38
-
39
 
40
  def respond(self, text_input, question, chat_history):
 
 
41
  if text_input.startswith("http"):
42
  response = requests.get(text_input)
43
  text_var = response.text
@@ -72,4 +76,4 @@ class DocumentChatbot:
72
 
73
  time.sleep(1)
74
 
75
- return "", chat_history
 
12
  from langchain import HuggingFaceHub
13
 
14
 
15
+ from os import environ
16
+
17
+
18
  class DocumentChatbot:
19
 
20
  def __init__(self):
 
24
  self.metadata = {"source": "internet"}
25
  self.init_mes = ["According to the document, ", "Based on the text, ", "I think, ", "According to the text, ", "Based on the document you provided, "]
26
 
27
+
28
+ def load_token_and_model(self, api_key, model_name):
29
+ result = None
30
  if api_key[:2] == "hf":
31
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = api_key
32
  result = subprocess.run(["curl", "https://huggingface.co/api/whoami-v2", "-H", f"Authorization: Bearer {api_key}"], capture_output=True).stdout.decode()
33
  if result == '{"error":"Invalid username or password."}':
34
  return "Invalid API token"
35
  else:
36
+ self.llm = HuggingFaceHub(repo_id=model_name, model_kwargs={"temperature":0, "max_length":512})
37
+ self.chain = load_qa_chain(self.llm, chain_type="stuff")
38
+ self.embeddings = HuggingFaceEmbeddings()
39
+ return "Model and Token successfully loaded"
40
 
 
 
 
 
 
 
41
 
42
  def respond(self, text_input, question, chat_history):
43
+ if not question or question.isspace():
44
+ return "Please enter a valid question.", chat_history
45
  if text_input.startswith("http"):
46
  response = requests.get(text_input)
47
  text_var = response.text
 
76
 
77
  time.sleep(1)
78
 
79
+ return "", chat_history