rairo commited on
Commit
183a32f
·
verified ·
1 Parent(s): 8db3e21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -58
app.py CHANGED
@@ -16,76 +16,26 @@ from PIL import Image
16
  import io
17
  import base64
18
 
19
-
20
-
21
  class StreamLitResponse(ResponseParser):
22
  def __init__(self, context):
23
  super().__init__(context)
 
24
 
25
  def format_dataframe(self, result):
26
- st.dataframe(result['value'])
27
  return
28
 
29
  def format_plot(self, result):
30
- try:
31
- image = result['value']
32
-
33
- if isinstance(image, Image.Image): # PIL Image
34
- img_bytes = io.BytesIO()
35
- image.save(img_bytes, format="PNG")
36
- img_bytes = img_bytes.getvalue()
37
- encoded = base64.b64encode(img_bytes).decode("ascii")
38
-
39
- st.image(image) # Display the image
40
- fig = go.Figure(data=[go.Image(source=f'data:image/png;base64,{encoded}')])
41
- fig.update_layout(
42
- margin=dict(l=0, r=0, b=0, t=0),
43
- xaxis=dict(visible=False),
44
- yaxis=dict(visible=False),
45
- )
46
- st.plotly_chart(fig)
47
-
48
- elif isinstance(image, bytes): # Bytes
49
- encoded = base64.b64encode(image).decode("ascii")
50
- fig = go.Figure(data=[go.Image(source=f'data:image/png;base64,{encoded}')])
51
- fig.update_layout(
52
- margin=dict(l=0, r=0, b=0, t=0),
53
- xaxis=dict(visible=False),
54
- yaxis=dict(visible=False),
55
- )
56
- st.plotly_chart(fig)
57
-
58
- elif isinstance(image, str) and os.path.exists(image): # File Path
59
- with open(image, "rb") as f:
60
- encoded = base64.b64encode(f.read()).decode("ascii")
61
- fig = go.Figure(data=[go.Image(source=f'data:image/png;base64,{encoded}')])
62
- fig.update_layout(
63
- margin=dict(l=0, r=0, b=0, t=0),
64
- xaxis=dict(visible=False),
65
- yaxis=dict(visible=False),
66
- )
67
- st.plotly_chart(fig)
68
-
69
- elif isinstance(image, str): # Base64 encoded string
70
- fig = go.Figure(data=[go.Image(source=f'data:image/png;base64,{image}')])
71
- fig.update_layout(
72
- margin=dict(l=0, r=0, b=0, t=0),
73
- xaxis=dict(visible=False),
74
- yaxis=dict(visible=False),
75
- )
76
- st.plotly_chart(fig)
77
-
78
- else:
79
- st.write("Unsupported Image format")
80
-
81
- except Exception as e:
82
- st.image(image)
83
- st.write(f"Error displaying image: {e}")
84
 
85
  def format_other(self, result):
86
- st.write(result['value'])
87
  return
88
 
 
 
 
89
 
90
  load_dotenv() # Load environment variables at the beginning
91
  GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY') #Use .get to handle if the variable is not present
 
16
  import io
17
  import base64
18
 
 
 
19
  class StreamLitResponse(ResponseParser):
20
  def __init__(self, context):
21
  super().__init__(context)
22
+ self.results = []
23
 
24
  def format_dataframe(self, result):
25
+ self.results.append({"type": "dataframe", "value": result['value']})
26
  return
27
 
28
  def format_plot(self, result):
29
+ self.results.append({"type": "plot", "value": result['value']})
30
+ return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  def format_other(self, result):
33
+ self.results.append({"type": "text", "value": result['value']})
34
  return
35
 
36
+ def get_results(self):
37
+ return self.results
38
+
39
 
40
  load_dotenv() # Load environment variables at the beginning
41
  GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY') #Use .get to handle if the variable is not present