peterpull commited on
Commit
2dc0026
·
1 Parent(s): e3f3aa3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -2,10 +2,13 @@ from gpt_index import GPTSimpleVectorIndex
2
  from langchain import OpenAI
3
  import gradio as gr
4
  from gradio import Interface, Textbox
 
5
  import os
6
  import datetime
7
- from datasets import load_dataset
8
- from huggingface_hub import HfFolder
 
 
9
 
10
  os.environ["OPENAI_API_KEY"] = os.environ['SECRET_CODE']
11
 
@@ -19,15 +22,19 @@ INDEX_FILE = os.path.join("data", INDEX_FILENAME)
19
  # we need a write access token.
20
  HF_TOKEN = os.environ.get("HF_TOKEN")
21
  print("HF TOKEN is none?", HF_TOKEN is None)
 
22
 
23
- # Clones the distant repo to the local repo
24
- dataset = load_dataset(DATASET_REPO_URL)
25
- dataset_folder = HfFolder(dataset._data_files["train"][0].path).path
26
- print(f"Dataset folder: {dataset_folder}")
27
- print(f"Dataset files: {os.listdir(dataset_folder)}")
 
 
 
28
 
29
  def generate_text() -> str:
30
- with open(os.path.join(dataset_folder, DATA_FILENAME)) as file:
31
  text = ""
32
  for line in file:
33
  row_parts = line.strip().split(",")
@@ -40,21 +47,24 @@ def generate_text() -> str:
40
 
41
  def store_message(chatinput: str, chatresponse: str):
42
  if chatinput and chatresponse:
43
- with open(os.path.join(dataset_folder, DATA_FILENAME), "a") as file:
44
- file.write(f"{datetime.datetime.now()},{chatinput},{chatresponse}\n")
45
- print(f"Wrote to datafile: {datetime.datetime.now()},{chatinput},{chatresponse}\n")
46
-
47
- # Push back to hub every N-th time the function is called
48
- if store_message.count_calls % 1 == 0:
49
- print("Pushing back to Hugging Face model hub")
50
- dataset.commit("Added new chat data") # Commit the changes
51
- store_message.count_calls += 1
 
 
 
 
52
 
53
  return generate_text()
54
 
55
- store_message.count_calls = 1 #initiases the count at one. We want to count how many messages stored before pushing back to repo.
56
-
57
- # gets the index file which is the context data
58
  def get_index(index_file_path):
59
  if os.path.exists(index_file_path):
60
  index_size = os.path.getsize(index_file_path)
@@ -82,7 +92,7 @@ with open('about.txt', 'r') as file:
82
 
83
  iface = Interface(
84
  fn=chatbot,
85
- inputs=Textbox("Enter your question"),
86
  outputs="text",
87
  title="AI Chatbot trained on J. Haynes mediation material, v0.5",
88
  description=about)
 
2
  from langchain import OpenAI
3
  import gradio as gr
4
  from gradio import Interface, Textbox
5
+ import sys
6
  import os
7
  import datetime
8
+ import huggingface_hub
9
+ from huggingface_hub import Repository
10
+ from datetime import datetime
11
+ import csv
12
 
13
  os.environ["OPENAI_API_KEY"] = os.environ['SECRET_CODE']
14
 
 
22
  # we need a write access token.
23
  HF_TOKEN = os.environ.get("HF_TOKEN")
24
  print("HF TOKEN is none?", HF_TOKEN is None)
25
+ print("HF hub ver", huggingface_hub.__version__)
26
 
27
+ #Clones the distant repo to the local repo
28
+ repo = Repository(
29
+ local_dir='data',
30
+ clone_from=DATASET_REPO_URL,
31
+ use_auth_token=HF_TOKEN)
32
+
33
+ print(f"Repo local_dir: {repo.local_dir}")
34
+ print(f"Repo files: {os.listdir(repo.local_dir)}")
35
 
36
  def generate_text() -> str:
37
+ with open(DATA_FILE) as file:
38
  text = ""
39
  for line in file:
40
  row_parts = line.strip().split(",")
 
47
 
48
  def store_message(chatinput: str, chatresponse: str):
49
  if chatinput and chatresponse:
50
+ with open(DATA_FILE, "a") as file:
51
+ file.write(f"{datetime.now()},{chatinput},{chatresponse}\n")
52
+ print(f"Wrote to datafile: {datetime.now()},{chatinput},{chatresponse}\n")
53
+
54
+ # Add the updated data file to the staged changes
55
+ repo.git_add(DATA_FILENAME)
56
+
57
+ # Commit the changes with a commit message
58
+ commit_message = "Added new data"
59
+ repo.git_commit(commit_message)
60
+
61
+ # Push the changes to the remote repository
62
+ repo.push(remote_name='origin', branch_name='main', use_auth_token=HF_TOKEN)
63
 
64
  return generate_text()
65
 
66
+
67
+ #gets the index file which is the context data
 
68
  def get_index(index_file_path):
69
  if os.path.exists(index_file_path):
70
  index_size = os.path.getsize(index_file_path)
 
92
 
93
  iface = Interface(
94
  fn=chatbot,
95
+ inputs=Textbox("Type here"),
96
  outputs="text",
97
  title="AI Chatbot trained on J. Haynes mediation material, v0.5",
98
  description=about)