CannaTech commited on
Commit
66bb11f
·
1 Parent(s): be1b89e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -23
app.py CHANGED
@@ -8,32 +8,23 @@ import json
8
  # Set the OpenAI API key
9
  openai.api_key = os.getenv("OPENAI_API_KEY")
10
 
11
- # Define the CustomHuggingFaceDatasetSaver class
12
- class HuggingFaceDatasetSaver(gr.FlaggingCallback):
13
- def setup(self):
14
- pass
15
-
16
- def apply(self, flagged_samples):
17
- for sample in flagged_samples:
18
- input_data = sample["input"]
19
- output_data = sample["output"]
20
- flag = sample["flag"]
21
 
22
- # Truncate the output if it exceeds 1000 characters
23
- if len(output_data) > 1000:
24
- output_data = output_data[:900] + " [Truncated]"
25
 
26
- # Save the data to Hugging Face dataset
27
- # Replace this placeholder logic with your own implementation
28
- # Example: Saving the data to a CSV file
29
- with open("flagged_data.csv", "a") as f:
30
- writer = csv.writer(f)
31
- writer.writerow([input_data, output_data, flag])
32
 
33
- # Set up the Hugging Face Dataset Saver
34
- HF_TOKEN = os.getenv("HF_TOKEN")
35
- hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "CannaTech/Flagged")
36
 
 
37
  # Define the authentication function
38
  def check_auth(username, password):
39
  # Get the credentials from the environment variable
@@ -150,7 +141,7 @@ iface = gr.Interface(
150
  favicon_path="https://assets.bigcartel.com/theme_images/101321509/IMG_6002.png", # Favicon image URL
151
  allow_flagging="manual", # Enable flagging with manual control
152
  flagging_options=["Incorrect"],
153
- flagging_callback=hf_writer
154
  )
155
 
156
  # Launch the interface with authentication
 
8
  # Set the OpenAI API key
9
  openai.api_key = os.getenv("OPENAI_API_KEY")
10
 
11
+ # Set up the Hugging Face Dataset Saver
12
+ HF_TOKEN = os.getenv("HF_TOKEN")
 
 
 
 
 
 
 
 
13
 
14
+ class CustomFlaggingCallback:
15
+ def __init__(self, hf_token, dataset_name):
16
+ self.hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "CannaTech/Flagged")
17
 
18
+ def __call__(self, input_data, output_data):
19
+ # Custom logic to handle flagged data
20
+ # Example: Save flagged data to Hugging Face Dataset without writing the output to JSON file
21
+ self.hf_writer.add_data(input_data)
22
+ self.hf_writer.flush()
 
23
 
24
+ # Create an instance of the custom flagging callback
25
+ custom_callback = CustomFlaggingCallback(HF_TOKEN, "CannaTech/Flagged")
 
26
 
27
+
28
  # Define the authentication function
29
  def check_auth(username, password):
30
  # Get the credentials from the environment variable
 
141
  favicon_path="https://assets.bigcartel.com/theme_images/101321509/IMG_6002.png", # Favicon image URL
142
  allow_flagging="manual", # Enable flagging with manual control
143
  flagging_options=["Incorrect"],
144
+ flagging_callback=custom_callback
145
  )
146
 
147
  # Launch the interface with authentication