Sg-at-srijan-us-kg commited on
Commit
2df1006
1 Parent(s): 837f660

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -3
app.py CHANGED
@@ -15,10 +15,16 @@ def respond(
15
  # Initialize messages with the system message
16
  messages = [{"role": "system", "content": system_message}]
17
 
18
- # Read file content if a file is uploaded
19
  if file:
20
  try:
21
- file_content = file.read().decode('utf-8') # Read and decode bytes to string
 
 
 
 
 
 
22
  print("File content:", file_content) # Debug print
23
  message = f"{file_content}\n\n{message}" # Append file content to message
24
  except Exception as e:
@@ -49,7 +55,6 @@ def respond(
49
  response += token
50
  yield response
51
 
52
- # Set up the Gradio interface
53
  demo = gr.ChatInterface(
54
  fn=respond,
55
  additional_inputs=[
@@ -69,3 +74,4 @@ demo = gr.ChatInterface(
69
 
70
  if __name__ == "__main__":
71
  demo.launch()
 
 
15
  # Initialize messages with the system message
16
  messages = [{"role": "system", "content": system_message}]
17
 
18
+ # Handle file content if a file is uploaded
19
  if file:
20
  try:
21
+ if hasattr(file, 'read'): # If file-like object, read it
22
+ file_content = file.read().decode('utf-8')
23
+ elif hasattr(file, 'value'): # If NamedString or similar, access `value`
24
+ file_content = file.value
25
+ else:
26
+ file_content = str(file) # Fallback to str conversion if neither works
27
+
28
  print("File content:", file_content) # Debug print
29
  message = f"{file_content}\n\n{message}" # Append file content to message
30
  except Exception as e:
 
55
  response += token
56
  yield response
57
 
 
58
  demo = gr.ChatInterface(
59
  fn=respond,
60
  additional_inputs=[
 
74
 
75
  if __name__ == "__main__":
76
  demo.launch()
77
+