bambadij commited on
Commit
b1435f0
·
verified ·
1 Parent(s): 48333ed
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -136,33 +136,45 @@ async def generate_text(request: RequestModel):
136
  return {"summary_text_2": generated_text}
137
  @app.post("/generate2/")
138
  async def generate_text(file: UploadFile = File(...)):
 
 
 
 
 
 
 
139
  # Read the uploaded CSV file
140
  try:
141
- contents = await file.read()
142
- df = pd.read_csv(StringIO(contents.decode('utf-8')))
143
  except Exception as e:
144
  return {"error": f"Error reading CSV file: {str(e)}"}
145
 
146
- # Concatenate all rows into a single string
 
 
 
 
147
  try:
148
- # Convert the entire DataFrame to a string
149
  text_to_generate = df.to_string(index=False)
150
  except Exception as e:
151
  return {"error": f"Error converting DataFrame to string: {str(e)}"}
152
 
 
 
 
 
153
  # Create the request for the API
154
  try:
155
  completion = client.chat.completions.create(
156
  model="meta/llama-3.1-8b-instruct",
157
- messages=[{"role": "user", "content": prompt1 + text_to_generate}],
158
  temperature=0.2,
159
  top_p=0.9,
160
- # max_tokens=1024,
161
  stream=True
162
  )
163
  except Exception as e:
164
  return {"error": f"Error generating text: {str(e)}"}
165
-
166
  generated_text = ""
167
  for chunk in completion:
168
  if chunk.choices[0].delta.content is not None:
 
136
  return {"summary_text_2": generated_text}
137
  @app.post("/generate2/")
138
  async def generate_text(file: UploadFile = File(...)):
139
+ # Check file size
140
+ contents = await file.read()
141
+ file_size = len(contents)
142
+
143
+ if file_size > 5_000_000: # 5MB limit
144
+ return {"error": "File size exceeds the 5MB limit. The file will be sampled."}
145
+
146
  # Read the uploaded CSV file
147
  try:
148
+ df = pd.read_csv(io.StringIO(contents.decode('utf-8')))
 
149
  except Exception as e:
150
  return {"error": f"Error reading CSV file: {str(e)}"}
151
 
152
+ # Sample the data if it's too large
153
+ if len(df) > 1000: # Adjust this number based on your needs
154
+ df = df.sample(n=100, random_state=42)
155
+
156
+ # Convert the DataFrame to a string
157
  try:
 
158
  text_to_generate = df.to_string(index=False)
159
  except Exception as e:
160
  return {"error": f"Error converting DataFrame to string: {str(e)}"}
161
 
162
+ # Ensure the generated text is within size limits
163
+ if len(text_to_generate.encode('utf-8')) > 5_000_000:
164
+ return {"error": "Generated text exceeds size limit even after sampling. Please reduce the data further."}
165
+
166
  # Create the request for the API
167
  try:
168
  completion = client.chat.completions.create(
169
  model="meta/llama-3.1-8b-instruct",
170
+ messages=[{"role": "user", "content": prompt1 + text_to_generate}],
171
  temperature=0.2,
172
  top_p=0.9,
 
173
  stream=True
174
  )
175
  except Exception as e:
176
  return {"error": f"Error generating text: {str(e)}"}
177
+
178
  generated_text = ""
179
  for chunk in completion:
180
  if chunk.choices[0].delta.content is not None: