jainarham commited on
Commit
7dbf9c8
·
verified ·
1 Parent(s): 8d076c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -20
app.py CHANGED
@@ -159,8 +159,27 @@ def api_search(query: str) -> dict:
159
  return {"result": results}
160
 
161
 
162
- # Create Gradio interface
163
- with gr.Blocks(title="AJ AI Backend", theme=gr.themes.Soft(primary_hue="purple")) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  gr.Markdown("""
165
  # 🤖 AJ AI Backend Server
166
 
@@ -176,19 +195,6 @@ with gr.Blocks(title="AJ AI Backend", theme=gr.themes.Soft(primary_hue="purple")
176
  submit = gr.Button("Send", variant="primary")
177
  clear = gr.Button("Clear")
178
 
179
- def respond(message, chat_history):
180
- response = chat(message, chat_history)
181
- try:
182
- parsed = json.loads(response)
183
- display_response = parsed.get("text", response)
184
- # Add action info if present
185
- if parsed.get("action"):
186
- display_response += f"\n\n📱 Action: `{json.dumps(parsed['action'])}`"
187
- except:
188
- display_response = response
189
- chat_history.append((message, display_response))
190
- return "", chat_history
191
-
192
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
193
  submit.click(respond, [msg, chatbot], [msg, chatbot])
194
  clear.click(lambda: None, None, chatbot, queue=False)
@@ -211,13 +217,21 @@ with gr.Blocks(title="AJ AI Backend", theme=gr.themes.Soft(primary_hue="purple")
211
  *Your own AI server - No API keys needed!*
212
  """)
213
 
214
- # Add API routes
215
- demo.queue()
216
-
217
- # FastAPI mounting for API endpoints
218
  from fastapi import FastAPI
 
 
219
  app = FastAPI()
220
 
 
 
 
 
 
 
 
 
 
221
  @app.post("/api/chat")
222
  async def chat_endpoint(data: dict):
223
  message = data.get("message", "")
@@ -229,5 +243,9 @@ async def search_endpoint(data: dict):
229
  query = data.get("query", "")
230
  return api_search(query)
231
 
232
- # Mount Gradio
233
  app = gr.mount_gradio_app(app, demo, path="/")
 
 
 
 
 
159
  return {"result": results}
160
 
161
 
162
+ def respond(message, chat_history):
163
+ """Handle chat responses"""
164
+ response = chat(message, chat_history)
165
+ try:
166
+ parsed = json.loads(response)
167
+ display_response = parsed.get("text", response)
168
+ # Add action info if present
169
+ if parsed.get("action"):
170
+ display_response += f"\n\n📱 Action: `{json.dumps(parsed['action'])}`"
171
+ except:
172
+ display_response = response
173
+ chat_history.append((message, display_response))
174
+ return "", chat_history
175
+
176
+
177
+ # ============================================
178
+ # GRADIO 6.0+ COMPATIBLE CODE (FIXED!)
179
+ # ============================================
180
+
181
+ # Create the interface WITHOUT theme in Blocks()
182
+ with gr.Blocks(title="AJ AI Backend") as demo:
183
  gr.Markdown("""
184
  # 🤖 AJ AI Backend Server
185
 
 
195
  submit = gr.Button("Send", variant="primary")
196
  clear = gr.Button("Clear")
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
199
  submit.click(respond, [msg, chatbot], [msg, chatbot])
200
  clear.click(lambda: None, None, chatbot, queue=False)
 
217
  *Your own AI server - No API keys needed!*
218
  """)
219
 
220
+ # Add API routes using FastAPI
 
 
 
221
  from fastapi import FastAPI
222
+ from fastapi.middleware.cors import CORSMiddleware
223
+
224
  app = FastAPI()
225
 
226
+ # Enable CORS so your app can connect
227
+ app.add_middleware(
228
+ CORSMiddleware,
229
+ allow_origins=["*"], # Your app can connect from anywhere
230
+ allow_credentials=True,
231
+ allow_methods=["*"],
232
+ allow_headers=["*"],
233
+ )
234
+
235
  @app.post("/api/chat")
236
  async def chat_endpoint(data: dict):
237
  message = data.get("message", "")
 
243
  query = data.get("query", "")
244
  return api_search(query)
245
 
246
+ # Mount Gradio app - theme goes in launch() for Gradio 6.0+
247
  app = gr.mount_gradio_app(app, demo, path="/")
248
+
249
+ # For running locally (optional)
250
+ if __name__ == "__main__":
251
+ demo.launch()