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

Update app.py via AI Editor

Browse files
Files changed (1) hide show
  1. app.py +2 -12
app.py CHANGED
@@ -9,38 +9,31 @@ import anthropic
9
  from threading import Thread
10
  import logging
11
 
12
- # Logging setup
13
  logging.basicConfig(
14
  level=logging.INFO,
15
  format='[%(asctime)s] %(levelname)s - %(message)s'
16
  )
17
 
18
- # Initialize Dash app
19
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
20
  server = app.server
21
 
22
- # Anthropic API setup
23
  ANTHROPIC_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
24
  anthropic_client = anthropic.Anthropic(api_key=ANTHROPIC_KEY)
25
  CLAUDE3_SONNET_MODEL = "claude-3-7-sonnet-20250219"
26
  CLAUDE3_MAX_CONTEXT_TOKENS = 200_000
27
  CLAUDE3_MAX_OUTPUT_TOKENS = 64_000
28
 
29
- # Global variables
30
  uploaded_document = None
31
  shredded_document = None
32
  generated_response = None
33
 
34
- # Helper functions
35
  def decode_document(decoded_bytes):
36
  try:
37
- # Try UTF-8 first
38
  content = decoded_bytes.decode('utf-8')
39
  logging.info("Document decoded as UTF-8.")
40
  return content
41
  except UnicodeDecodeError as e_utf8:
42
  try:
43
- # Try latin-1 fallback
44
  content = decoded_bytes.decode('latin-1')
45
  logging.warning("Document decoded as Latin-1 due to utf-8 decode error: %s", e_utf8)
46
  return content
@@ -74,13 +67,13 @@ def process_document(content, filename, action):
74
  "Do not write as if responding to the proposal:\n\n" + uploaded_document
75
  )
76
  def thread_shred():
 
77
  try:
78
  response = anthropic_client.messages.create(
79
  model=CLAUDE3_SONNET_MODEL,
80
  max_tokens=CLAUDE3_MAX_OUTPUT_TOKENS,
81
  messages=[{"role": "user", "content": prompt}]
82
  )
83
- nonlocal shredded_document
84
  shredded_document = response.content[0].text
85
  logging.info("Document shredded successfully.")
86
  except Exception as e:
@@ -102,13 +95,13 @@ def process_document(content, filename, action):
102
  "Refer to research that validates the approach and cite sources with measurable outcomes:\n\n" + shredded_document
103
  )
104
  def thread_generate():
 
105
  try:
106
  response = anthropic_client.messages.create(
107
  model=CLAUDE3_SONNET_MODEL,
108
  max_tokens=CLAUDE3_MAX_OUTPUT_TOKENS,
109
  messages=[{"role": "user", "content": prompt}]
110
  )
111
- nonlocal generated_response
112
  generated_response = response.content[0].text
113
  logging.info("Proposal response generated successfully.")
114
  except Exception as e:
@@ -122,7 +115,6 @@ def process_document(content, filename, action):
122
 
123
  return "Action not implemented yet."
124
 
125
- # Layout
126
  app.layout = dbc.Container([
127
  dbc.Row([
128
  dbc.Col([
@@ -185,7 +177,6 @@ app.layout = dbc.Container([
185
  ], style={'marginTop':'20px'})
186
  ], fluid=True)
187
 
188
- # Callback for document upload
189
  @app.callback(
190
  Output('output-document-upload', 'children'),
191
  Input('upload-document', 'contents'),
@@ -199,7 +190,6 @@ def handle_upload(content, filename):
199
  return html.Div(result, style={"wordWrap": "break-word"})
200
  return ""
201
 
202
- # Unified callback for all actions
203
  @app.callback(
204
  Output('output-data-upload', 'children'),
205
  [
 
9
  from threading import Thread
10
  import logging
11
 
 
12
  logging.basicConfig(
13
  level=logging.INFO,
14
  format='[%(asctime)s] %(levelname)s - %(message)s'
15
  )
16
 
 
17
  app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
18
  server = app.server
19
 
 
20
  ANTHROPIC_KEY = os.environ.get("ANTHROPIC_API_KEY", "")
21
  anthropic_client = anthropic.Anthropic(api_key=ANTHROPIC_KEY)
22
  CLAUDE3_SONNET_MODEL = "claude-3-7-sonnet-20250219"
23
  CLAUDE3_MAX_CONTEXT_TOKENS = 200_000
24
  CLAUDE3_MAX_OUTPUT_TOKENS = 64_000
25
 
 
26
  uploaded_document = None
27
  shredded_document = None
28
  generated_response = None
29
 
 
30
  def decode_document(decoded_bytes):
31
  try:
 
32
  content = decoded_bytes.decode('utf-8')
33
  logging.info("Document decoded as UTF-8.")
34
  return content
35
  except UnicodeDecodeError as e_utf8:
36
  try:
 
37
  content = decoded_bytes.decode('latin-1')
38
  logging.warning("Document decoded as Latin-1 due to utf-8 decode error: %s", e_utf8)
39
  return content
 
67
  "Do not write as if responding to the proposal:\n\n" + uploaded_document
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:
 
95
  "Refer to research that validates the approach and cite sources with measurable outcomes:\n\n" + shredded_document
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:
 
115
 
116
  return "Action not implemented yet."
117
 
 
118
  app.layout = dbc.Container([
119
  dbc.Row([
120
  dbc.Col([
 
177
  ], style={'marginTop':'20px'})
178
  ], fluid=True)
179
 
 
180
  @app.callback(
181
  Output('output-document-upload', 'children'),
182
  Input('upload-document', 'contents'),
 
190
  return html.Div(result, style={"wordWrap": "break-word"})
191
  return ""
192
 
 
193
  @app.callback(
194
  Output('output-data-upload', 'children'),
195
  [