Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,13 +2,10 @@ from gpt_index import GPTSimpleVectorIndex
|
|
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
|
9 |
-
from huggingface_hub import
|
10 |
-
from datetime import datetime
|
11 |
-
import csv
|
12 |
|
13 |
os.environ["OPENAI_API_KEY"] = os.environ['SECRET_CODE']
|
14 |
|
@@ -22,19 +19,15 @@ INDEX_FILE = os.path.join("data", INDEX_FILENAME)
|
|
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 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
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(
|
38 |
text = ""
|
39 |
for line in file:
|
40 |
row_parts = line.strip().split(",")
|
@@ -45,30 +38,23 @@ def generate_text() -> str:
|
|
45 |
return text if text else "No messages yet"
|
46 |
|
47 |
|
48 |
-
def push_to_hub(commit_message):
|
49 |
-
repo.git_add(DATA_FILE) # Add the updated data file to the staged changes
|
50 |
-
repo.git_commit(commit_message) # Commit the changes
|
51 |
-
repo.push_to_hub(token=HF_TOKEN) # Push the changes to the remote repository
|
52 |
-
|
53 |
-
|
54 |
def store_message(chatinput: str, chatresponse: str):
|
55 |
if chatinput and chatresponse:
|
56 |
-
with open(
|
57 |
-
file.write(f"{datetime.now()},{chatinput},{chatresponse}\n")
|
58 |
-
print(f"Wrote to datafile: {datetime.now()},{chatinput},{chatresponse}\n")
|
59 |
|
60 |
# Push back to hub every N-th time the function is called
|
61 |
if store_message.count_calls % 1 == 0:
|
62 |
print("Pushing back to Hugging Face model hub")
|
63 |
-
|
64 |
-
push_to_hub(commit_message="Added new chat data") # Include commit_message parameter
|
65 |
store_message.count_calls += 1
|
66 |
|
67 |
return generate_text()
|
68 |
|
69 |
store_message.count_calls = 1 #initiases the count at one. We want to count how many messages stored before pushing back to repo.
|
70 |
|
71 |
-
#gets the index file which is the context data
|
72 |
def get_index(index_file_path):
|
73 |
if os.path.exists(index_file_path):
|
74 |
index_size = os.path.getsize(index_file_path)
|
|
|
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 |
# 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(",")
|
|
|
38 |
return text if text else "No messages yet"
|
39 |
|
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)
|