eshan6704 commited on
Commit
ab4dad1
·
verified ·
1 Parent(s): 80d5cbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -35
app.py CHANGED
@@ -1,4 +1,10 @@
1
- # Always explicitly import modules used in your endpoint
 
 
 
 
 
 
2
  import common
3
  import stock
4
  import indices_html
@@ -33,7 +39,7 @@ STOCK_REQ = [
33
 
34
  INDEX_REQ = [
35
  "indices", "nse_open", "nse_preopen", "nse_fno", "nse_fiidii",
36
- "nse_events", "nse_future", "nse_bhav", "nse_highlow","stock_highlow",
37
  "index_history", "nse_largedeals", "nse_most_active",
38
  "largedeals_historical", "nse_bulkdeals", "nse_blockdeals",
39
  "index_pe_pb_div", "index_total_returns"
@@ -59,19 +65,47 @@ def handle_stock(req: FetchRequest):
59
  raise HTTPException(status_code=400, detail=f"Invalid stock req_type: {req.req_type}")
60
 
61
  t = req.req_type.lower()
62
- if t == "info": return common.wrap(yahooinfo.fetch_info(req.name))
63
- if t == "intraday": return common.wrap(stock.fetch_intraday(req.name, req.date_start, req.date_end))
64
- if t == "daily": return common.wrap(stock.fetch_daily(req.name, req.date_start, req.date_end))
65
- if t == "nse_eq": return eq_html.build_eq_html(req.name)
66
- if t == "qresult": return common.wrap(stock.fetch_qresult(req.name))
67
- if t == "result": return common.wrap(stock.fetch_result(req.name))
68
- if t == "balance": return common.wrap(stock.fetch_balance(req.name))
69
- if t == "cashflow": return common.wrap(stock.fetch_cashflow(req.name))
70
- if t == "dividend": return common.wrap(stock.fetch_dividend(req.name))
71
- if t == "split": return common.wrap(stock.fetch_split(req.name))
72
- if t == "other": return common.wrap(stock.fetch_other(req.name))
73
- if t == "stock_hist": return nsepython.nse_stock_hist(req.date_start, req.date_end, req.name).to_html()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  return common.wrap(f"<h3>No handler for stock req_type: {t}</h3>")
 
75
  except Exception as e:
76
  return {"error": str(e)}
77
 
@@ -82,25 +116,71 @@ def handle_index(req: FetchRequest):
82
  raise HTTPException(status_code=400, detail=f"Invalid index req_type: {req.req_type}")
83
 
84
  t = req.req_type.lower()
85
- if t == "indices": return indices_html.build_indices_html()
86
- if t == "nse_open": return index_live_html.build_index_live_html()
87
- if t == "nse_preopen": return preopen_html.build_preopen_html()
88
- if t == "nse_fno": return build_nse_fno.nse_fno_html(req.date_end, req.name)
89
- if t == "nse_fiidii": return nsepython.nse_fiidii().to_html()
90
- if t == "nse_events": return nsepython.nse_events().to_html()
91
- if t == "nse_future": return common.wrap(nsepython.nse_future(req.name))
92
- if t == "nse_highlow": return nsepython.nse_highlow(req.date_end).to_html()
93
- if t == "stock_highlow": return nsepython.stock_highlow(req.date_end).to_html()
94
- if t == "nse_bhav": return bhavcopy_html.build_bhavcopy_html(req.date_end)
95
- if t == "nse_largedeals": return nsepython.nse_largedeals().to_html()
96
- if t == "nse_bulkdeals": return nsepython.nse_bulkdeals().to_html()
97
- if t == "nse_blockdeals": return nsepython.nse_blockdeals().to_html()
98
- if t == "nse_most_active": return nsepython.nse_most_active().to_html()
99
- if t == "index_history": return nsepython.index_history("NIFTY", req.date_start, req.date_end).to_html()
100
- if t == "largedeals_historical": return nsepython.nse_largedeals_historical(req.date_start, req.date_end).to_html()
101
- if t == "index_pe_pb_div": return nsepython.index_pe_pb_div("NIFTY", req.date_start, req.date_end).to_html()
102
- if t == "index_total_returns": return nsepython.index_total_returns("NIFTY", req.date_start, req.date_end).to_html()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  return common.wrap(f"<h3>No handler for index req_type: {t}</h3>")
 
104
  except Exception as e:
105
  return {"error": str(e)}
106
 
@@ -108,9 +188,11 @@ def handle_index(req: FetchRequest):
108
  @app.post("/api/fetch")
109
  def fetch_data(req: FetchRequest):
110
  mode = req.mode.lower()
 
111
  if mode == "stock":
112
  return handle_stock(req)
113
- elif mode == "index":
 
114
  return handle_index(req)
115
- else:
116
- raise HTTPException(status_code=400, detail=f"Invalid mode: {req.mode}")
 
1
+ import sys
2
+ import os
3
+
4
+ # 🔑 CRITICAL for Hugging Face Spaces
5
+ sys.path.append(os.path.dirname(__file__))
6
+
7
+ # ---------- Always explicitly import local modules ----------
8
  import common
9
  import stock
10
  import indices_html
 
39
 
40
  INDEX_REQ = [
41
  "indices", "nse_open", "nse_preopen", "nse_fno", "nse_fiidii",
42
+ "nse_events", "nse_future", "nse_bhav", "nse_highlow", "stock_highlow",
43
  "index_history", "nse_largedeals", "nse_most_active",
44
  "largedeals_historical", "nse_bulkdeals", "nse_blockdeals",
45
  "index_pe_pb_div", "index_total_returns"
 
65
  raise HTTPException(status_code=400, detail=f"Invalid stock req_type: {req.req_type}")
66
 
67
  t = req.req_type.lower()
68
+
69
+ if t == "info":
70
+ return common.wrap(yahooinfo.fetch_info(req.name))
71
+
72
+ if t == "intraday":
73
+ return common.wrap(stock.fetch_intraday(req.name, req.date_start, req.date_end))
74
+
75
+ if t == "daily":
76
+ return common.wrap(stock.fetch_daily(req.name, req.date_start, req.date_end))
77
+
78
+ if t == "nse_eq":
79
+ return eq_html.build_eq_html(req.name)
80
+
81
+ if t == "qresult":
82
+ return common.wrap(stock.fetch_qresult(req.name))
83
+
84
+ if t == "result":
85
+ return common.wrap(stock.fetch_result(req.name))
86
+
87
+ if t == "balance":
88
+ return common.wrap(stock.fetch_balance(req.name))
89
+
90
+ if t == "cashflow":
91
+ return common.wrap(stock.fetch_cashflow(req.name))
92
+
93
+ if t == "dividend":
94
+ return common.wrap(stock.fetch_dividend(req.name))
95
+
96
+ if t == "split":
97
+ return common.wrap(stock.fetch_split(req.name))
98
+
99
+ if t == "other":
100
+ return common.wrap(stock.fetch_other(req.name))
101
+
102
+ if t == "stock_hist":
103
+ return nsepython.nse_stock_hist(
104
+ req.date_start, req.date_end, req.name
105
+ ).to_html()
106
+
107
  return common.wrap(f"<h3>No handler for stock req_type: {t}</h3>")
108
+
109
  except Exception as e:
110
  return {"error": str(e)}
111
 
 
116
  raise HTTPException(status_code=400, detail=f"Invalid index req_type: {req.req_type}")
117
 
118
  t = req.req_type.lower()
119
+
120
+ if t == "indices":
121
+ return indices_html.build_indices_html()
122
+
123
+ if t == "nse_open":
124
+ return index_live_html.build_index_live_html()
125
+
126
+ if t == "nse_preopen":
127
+ return preopen_html.build_preopen_html()
128
+
129
+ if t == "nse_fno":
130
+ return build_nse_fno.nse_fno_html(req.date_end, req.name)
131
+
132
+ if t == "nse_fiidii":
133
+ return nsepython.nse_fiidii().to_html()
134
+
135
+ if t == "nse_events":
136
+ return nsepython.nse_events().to_html()
137
+
138
+ if t == "nse_future":
139
+ return common.wrap(nsepython.nse_future(req.name))
140
+
141
+ if t == "nse_highlow":
142
+ return nsepython.nse_highlow(req.date_end).to_html()
143
+
144
+ if t == "stock_highlow":
145
+ return nsepython.stock_highlow(req.date_end).to_html()
146
+
147
+ if t == "nse_bhav":
148
+ return bhavcopy_html.build_bhavcopy_html(req.date_end)
149
+
150
+ if t == "nse_largedeals":
151
+ return nsepython.nse_largedeals().to_html()
152
+
153
+ if t == "nse_bulkdeals":
154
+ return nsepython.nse_bulkdeals().to_html()
155
+
156
+ if t == "nse_blockdeals":
157
+ return nsepython.nse_blockdeals().to_html()
158
+
159
+ if t == "nse_most_active":
160
+ return nsepython.nse_most_active().to_html()
161
+
162
+ if t == "index_history":
163
+ return nsepython.index_history(
164
+ "NIFTY", req.date_start, req.date_end
165
+ ).to_html()
166
+
167
+ if t == "largedeals_historical":
168
+ return nsepython.nse_largedeals_historical(
169
+ req.date_start, req.date_end
170
+ ).to_html()
171
+
172
+ if t == "index_pe_pb_div":
173
+ return nsepython.index_pe_pb_div(
174
+ "NIFTY", req.date_start, req.date_end
175
+ ).to_html()
176
+
177
+ if t == "index_total_returns":
178
+ return nsepython.index_total_returns(
179
+ "NIFTY", req.date_start, req.date_end
180
+ ).to_html()
181
+
182
  return common.wrap(f"<h3>No handler for index req_type: {t}</h3>")
183
+
184
  except Exception as e:
185
  return {"error": str(e)}
186
 
 
188
  @app.post("/api/fetch")
189
  def fetch_data(req: FetchRequest):
190
  mode = req.mode.lower()
191
+
192
  if mode == "stock":
193
  return handle_stock(req)
194
+
195
+ if mode == "index":
196
  return handle_index(req)
197
+
198
+ raise HTTPException(status_code=400, detail=f"Invalid mode: {req.mode}")