TeacherPuffy commited on
Commit
d55b380
·
verified ·
1 Parent(s): d7d471b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -8
app.py CHANGED
@@ -55,22 +55,22 @@ def segment_text(file_path):
55
  logger.info(f"Segmented text into {len(chunks)} chunks.")
56
  return chunks
57
 
58
- # Function to process the text file and make parallel API calls
59
  def process_text(file, prompt):
60
  try:
61
  logger.info("Starting text processing...")
62
 
63
  # Segment the text file into chunks
64
- chunks = segment_text(file.name)
 
65
 
66
- # Perform two parallel API calls for each chunk
67
  results = []
68
  for idx, chunk in enumerate(chunks):
69
  logger.info(f"Processing chunk {idx + 1}/{len(chunks)}")
70
  try:
71
- result1 = call_api(f"{prompt}\n\n{chunk}")
72
- result2 = call_api(f"{prompt}\n\n{chunk}")
73
- results.extend([result1, result2])
74
  logger.info(f"Chunk {idx + 1} processed successfully.")
75
  except Exception as e:
76
  logger.error(f"Failed to process chunk {idx + 1}: {e}")
@@ -87,7 +87,9 @@ def process_text(file, prompt):
87
  # Upload to Hugging Face dataset
88
  try:
89
  logger.info("Uploading results to Hugging Face dataset...")
90
- hf_api = HfApi(token=os.environ["HUGGINGFACE_TOKEN"])
 
 
91
  dataset = Dataset.from_dict({"text": results})
92
  dataset.push_to_hub("TeacherPuffy/book") # Updated dataset name
93
  logger.info("Results uploaded to Hugging Face dataset successfully.")
@@ -115,7 +117,7 @@ def process_text(file, prompt):
115
 
116
  # Gradio interface
117
  with gr.Blocks() as demo:
118
- gr.Markdown("## Text File Processor with Parallel API Calls")
119
  with gr.Row():
120
  file_input = gr.File(label="Upload Text File")
121
  prompt_input = gr.Textbox(label="Enter Prompt")
 
55
  logger.info(f"Segmented text into {len(chunks)} chunks.")
56
  return chunks
57
 
58
+ # Function to process the text file and make API calls
59
  def process_text(file, prompt):
60
  try:
61
  logger.info("Starting text processing...")
62
 
63
  # Segment the text file into chunks
64
+ file_path = file.name if hasattr(file, "name") else file
65
+ chunks = segment_text(file_path)
66
 
67
+ # Perform API calls for each chunk
68
  results = []
69
  for idx, chunk in enumerate(chunks):
70
  logger.info(f"Processing chunk {idx + 1}/{len(chunks)}")
71
  try:
72
+ result = call_api(f"{prompt}\n\n{chunk}")
73
+ results.append(result)
 
74
  logger.info(f"Chunk {idx + 1} processed successfully.")
75
  except Exception as e:
76
  logger.error(f"Failed to process chunk {idx + 1}: {e}")
 
87
  # Upload to Hugging Face dataset
88
  try:
89
  logger.info("Uploading results to Hugging Face dataset...")
90
+ hf_api = HfApi(token=os.environ.get("HUGGINGFACE_TOKEN"))
91
+ if not hf_api.token:
92
+ raise ValueError("Hugging Face token not found in environment variables.")
93
  dataset = Dataset.from_dict({"text": results})
94
  dataset.push_to_hub("TeacherPuffy/book") # Updated dataset name
95
  logger.info("Results uploaded to Hugging Face dataset successfully.")
 
117
 
118
  # Gradio interface
119
  with gr.Blocks() as demo:
120
+ gr.Markdown("## Text File Processor with API Calls")
121
  with gr.Row():
122
  file_input = gr.File(label="Upload Text File")
123
  prompt_input = gr.Textbox(label="Enter Prompt")