bluenevus commited on
Commit
72252ab
·
1 Parent(s): a1c3308

Update app.py via AI Editor

Browse files
Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -41,10 +41,29 @@ def decode_document(decoded_bytes):
41
  logging.error("Document decode failed for both utf-8 and latin-1: %s", e)
42
  return None
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  def process_document(content, filename, action):
45
  global uploaded_document, shredded_document, generated_response
46
  logging.info(f"Process document called with action: {action}")
47
-
48
  if action == 'upload':
49
  if content is None:
50
  return "No content uploaded."
@@ -56,7 +75,7 @@ def process_document(content, filename, action):
56
  uploaded_document = text
57
  logging.info("Document uploaded successfully.")
58
  return "Document uploaded successfully."
59
-
60
  elif action == 'shred':
61
  if not uploaded_document:
62
  logging.warning("No uploaded document found for shredding.")
@@ -68,13 +87,11 @@ def process_document(content, filename, action):
68
  )
69
  def thread_shred():
70
  global shredded_document
 
71
  try:
72
- response = anthropic_client.messages.create(
73
- model=CLAUDE3_SONNET_MODEL,
74
- max_tokens=CLAUDE3_MAX_OUTPUT_TOKENS,
75
- messages=[{"role": "user", "content": prompt}]
76
- )
77
- shredded_document = response.content[0].text
78
  logging.info("Document shredded successfully.")
79
  except Exception as e:
80
  shredded_document = f"Error during shredding: {e}"
@@ -84,7 +101,7 @@ def process_document(content, filename, action):
84
  t.start()
85
  t.join()
86
  return shredded_document
87
-
88
  elif action == 'generate':
89
  if not shredded_document:
90
  logging.warning("No shredded document found when generating response.")
@@ -96,13 +113,11 @@ def process_document(content, filename, action):
96
  )
97
  def thread_generate():
98
  global generated_response
 
99
  try:
100
- response = anthropic_client.messages.create(
101
- model=CLAUDE3_SONNET_MODEL,
102
- max_tokens=CLAUDE3_MAX_OUTPUT_TOKENS,
103
- messages=[{"role": "user", "content": prompt}]
104
- )
105
- generated_response = response.content[0].text
106
  logging.info("Proposal response generated successfully.")
107
  except Exception as e:
108
  generated_response = f"Error during generation: {e}"
@@ -112,7 +127,7 @@ def process_document(content, filename, action):
112
  t.start()
113
  t.join()
114
  return generated_response
115
-
116
  return "Action not implemented yet."
117
 
118
  app.layout = dbc.Container([
 
41
  logging.error("Document decode failed for both utf-8 and latin-1: %s", e)
42
  return None
43
 
44
+ def anthropic_stream_generate(prompt):
45
+ stream_result = []
46
+ try:
47
+ with anthropic_client.messages.create(
48
+ model=CLAUDE3_SONNET_MODEL,
49
+ max_tokens=CLAUDE3_MAX_OUTPUT_TOKENS,
50
+ messages=[{"role": "user", "content": prompt}],
51
+ stream=True
52
+ ) as stream:
53
+ for event in stream:
54
+ if event.type == "content_block_delta":
55
+ piece = event.delta.text
56
+ stream_result.append(piece)
57
+ logging.debug(f"Streaming piece: {piece}")
58
+ return ''.join(stream_result)
59
+ except Exception as e:
60
+ logging.error("Error during anthropic streaming request: %s", e)
61
+ return f"Error during streaming: {e}"
62
+
63
  def process_document(content, filename, action):
64
  global uploaded_document, shredded_document, generated_response
65
  logging.info(f"Process document called with action: {action}")
66
+
67
  if action == 'upload':
68
  if content is None:
69
  return "No content uploaded."
 
75
  uploaded_document = text
76
  logging.info("Document uploaded successfully.")
77
  return "Document uploaded successfully."
78
+
79
  elif action == 'shred':
80
  if not uploaded_document:
81
  logging.warning("No uploaded document found for shredding.")
 
87
  )
88
  def thread_shred():
89
  global shredded_document
90
+ shredded_document = ""
91
  try:
92
+ logging.info("Starting streaming shredding operation with Anthropics.")
93
+ result = anthropic_stream_generate(prompt)
94
+ shredded_document = result
 
 
 
95
  logging.info("Document shredded successfully.")
96
  except Exception as e:
97
  shredded_document = f"Error during shredding: {e}"
 
101
  t.start()
102
  t.join()
103
  return shredded_document
104
+
105
  elif action == 'generate':
106
  if not shredded_document:
107
  logging.warning("No shredded document found when generating response.")
 
113
  )
114
  def thread_generate():
115
  global generated_response
116
+ generated_response = ""
117
  try:
118
+ logging.info("Starting streaming generation operation with Anthropics.")
119
+ result = anthropic_stream_generate(prompt)
120
+ generated_response = result
 
 
 
121
  logging.info("Proposal response generated successfully.")
122
  except Exception as e:
123
  generated_response = f"Error during generation: {e}"
 
127
  t.start()
128
  t.join()
129
  return generated_response
130
+
131
  return "Action not implemented yet."
132
 
133
  app.layout = dbc.Container([