Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -17,7 +17,6 @@ logging.basicConfig(filename='debug.log', level=logging.DEBUG, format='%(asctime
|
|
17 |
processor = AutoProcessor.from_pretrained("mobenta/chart_analysis")
|
18 |
model = AutoModelForPreTraining.from_pretrained("mobenta/chart_analysis")
|
19 |
|
20 |
-
@spaces.GPU
|
21 |
def predict(image, input_text):
|
22 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
23 |
model.to(device)
|
@@ -132,47 +131,23 @@ def create_stock_chart(data, ticker, filename='chart.png', timeframe='1d', indic
|
|
132 |
resized_image = image.resize(new_size, Image.LANCZOS)
|
133 |
resized_image.save(filename)
|
134 |
|
135 |
-
logging.debug(f"Resized image
|
|
|
136 |
except Exception as e:
|
137 |
-
logging.error(f"Error creating
|
138 |
-
raise
|
139 |
-
|
140 |
-
def combine_images(image_paths, output_path='combined_chart.png'):
|
141 |
-
try:
|
142 |
-
logging.debug(f"Combining images {image_paths} into {output_path}")
|
143 |
-
images = [Image.open(path) for path in image_paths]
|
144 |
-
|
145 |
-
# Calculate total width and max height for combined image
|
146 |
-
total_width = sum(img.width for img in images)
|
147 |
-
max_height = max(img.height for img in images)
|
148 |
-
|
149 |
-
combined_image = Image.new('RGB', (total_width, max_height))
|
150 |
-
x_offset = 0
|
151 |
-
for img in images:
|
152 |
-
combined_image.paste(img, (x_offset, 0))
|
153 |
-
x_offset += img.width
|
154 |
-
|
155 |
-
combined_image.save(output_path)
|
156 |
-
logging.debug(f"Combined image saved to {output_path}")
|
157 |
-
return output_path
|
158 |
-
except Exception as e:
|
159 |
-
logging.error(f"Error combining images: {e}")
|
160 |
raise
|
161 |
|
162 |
def gradio_interface(ticker1, ticker2, ticker3, ticker4, start_date, end_date, query, analysis_type, interval, indicators):
|
163 |
try:
|
164 |
-
logging.debug(f"Starting gradio_interface with tickers: {ticker1}, {ticker2}, {ticker3}, {ticker4}, start_date: {start_date}, end_date: {end_date}, query: {query}, analysis_type: {analysis_type}, interval: {interval}")
|
165 |
-
|
166 |
-
tickers = [ticker1, ticker2, ticker3, ticker4]
|
167 |
chart_paths = []
|
|
|
168 |
|
169 |
-
for
|
170 |
-
if ticker:
|
171 |
-
data = fetch_stock_data(ticker,
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
chart_paths.append(chart_path)
|
176 |
|
177 |
if analysis_type == 'Comparative Analysis' and len(chart_paths) > 1:
|
178 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_combined_chart:
|
@@ -181,7 +156,6 @@ def gradio_interface(ticker1, ticker2, ticker3, ticker4, start_date, end_date, q
|
|
181 |
insights = predict(Image.open(combined_chart_path), query)
|
182 |
return insights, combined_chart_path
|
183 |
|
184 |
-
# No comparative analysis, just return the single chart
|
185 |
if chart_paths:
|
186 |
insights = predict(Image.open(chart_paths[0]), query)
|
187 |
return insights, chart_paths[0]
|
@@ -224,4 +198,4 @@ def gradio_app():
|
|
224 |
demo.launch()
|
225 |
|
226 |
if __name__ == "__main__":
|
227 |
-
gradio_app()
|
|
|
17 |
processor = AutoProcessor.from_pretrained("mobenta/chart_analysis")
|
18 |
model = AutoModelForPreTraining.from_pretrained("mobenta/chart_analysis")
|
19 |
|
|
|
20 |
def predict(image, input_text):
|
21 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
model.to(device)
|
|
|
131 |
resized_image = image.resize(new_size, Image.LANCZOS)
|
132 |
resized_image.save(filename)
|
133 |
|
134 |
+
logging.debug(f"Resized image saved to {filename}")
|
135 |
+
return filename
|
136 |
except Exception as e:
|
137 |
+
logging.error(f"Error creating stock chart: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
raise
|
139 |
|
140 |
def gradio_interface(ticker1, ticker2, ticker3, ticker4, start_date, end_date, query, analysis_type, interval, indicators):
|
141 |
try:
|
|
|
|
|
|
|
142 |
chart_paths = []
|
143 |
+
tickers = [ticker1, ticker2, ticker3, ticker4]
|
144 |
|
145 |
+
for ticker in tickers:
|
146 |
+
if ticker.strip():
|
147 |
+
data = fetch_stock_data(ticker, start_date, end_date, interval)
|
148 |
+
chart_path = f"{ticker}_chart.png"
|
149 |
+
create_stock_chart(data, ticker, filename=chart_path, timeframe=interval, indicators=indicators)
|
150 |
+
chart_paths.append(chart_path)
|
|
|
151 |
|
152 |
if analysis_type == 'Comparative Analysis' and len(chart_paths) > 1:
|
153 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_combined_chart:
|
|
|
156 |
insights = predict(Image.open(combined_chart_path), query)
|
157 |
return insights, combined_chart_path
|
158 |
|
|
|
159 |
if chart_paths:
|
160 |
insights = predict(Image.open(chart_paths[0]), query)
|
161 |
return insights, chart_paths[0]
|
|
|
198 |
demo.launch()
|
199 |
|
200 |
if __name__ == "__main__":
|
201 |
+
gradio_app()
|