RihemXX commited on
Commit
99f4a42
·
verified ·
1 Parent(s): fe0fb7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -649,17 +649,16 @@ api_interface = gr.Interface(
649
  api_name="/chat"
650
  )
651
 
652
- def http_api_infer(image, question):
653
- """
654
- Simple API endpoint that mimics InternVL logic with one image + text.
655
- """
656
- # Build a simplified version of `state` object here
657
  dummy_state = init_state()
658
  dummy_state.set_system_message("You are a vision-language assistant.")
659
  dummy_state.append_message(Conversation.USER, question, [image])
660
  dummy_state.skip_next = False
661
 
662
- # Simulate inference (you can directly call your model function here instead)
663
  worker_addr = os.environ.get("WORKER_ADDR", "")
664
  api_token = os.environ.get("API_TOKEN", "")
665
  headers = {
@@ -670,8 +669,6 @@ def http_api_infer(image, question):
670
  if not worker_addr:
671
  return "⚠️ Model backend is not configured."
672
 
673
- all_image_paths = [dummy_state.save_image(image)]
674
-
675
  pload = {
676
  "model": "InternVL2.5-78B",
677
  "messages": dummy_state.get_prompt_v2(inlude_image=True, max_dynamic_patch=12),
@@ -687,7 +684,7 @@ def http_api_infer(image, question):
687
  reply = response.json()["choices"][0]["message"]["content"]
688
  return reply
689
  except Exception as e:
690
- return f"Error: {str(e)}"
691
 
692
  if __name__ == "__main__":
693
  parser = argparse.ArgumentParser()
@@ -699,10 +696,28 @@ if __name__ == "__main__":
699
  args = parser.parse_args()
700
  logger.info(f"args: {args}")
701
 
702
- logger.info(args)
 
 
 
 
 
 
 
 
 
 
703
  demo = gr.TabbedInterface(
704
- interface_list=[build_demo(), api_interface],
705
- tab_names=["UI", "API"]
706
- )
707
- demo.queue(api_open=True).launch(...)
 
 
 
 
 
 
 
 
708
 
 
649
  api_name="/chat"
650
  )
651
 
652
+ def http_api_infer(image, question, request: gr.Request):
653
+ token = request.headers.get("x-api-token", "")
654
+ if token != "123456789Rihem":
655
+ return "❌ Unauthorized. Missing or invalid API token."
656
+
657
  dummy_state = init_state()
658
  dummy_state.set_system_message("You are a vision-language assistant.")
659
  dummy_state.append_message(Conversation.USER, question, [image])
660
  dummy_state.skip_next = False
661
 
 
662
  worker_addr = os.environ.get("WORKER_ADDR", "")
663
  api_token = os.environ.get("API_TOKEN", "")
664
  headers = {
 
669
  if not worker_addr:
670
  return "⚠️ Model backend is not configured."
671
 
 
 
672
  pload = {
673
  "model": "InternVL2.5-78B",
674
  "messages": dummy_state.get_prompt_v2(inlude_image=True, max_dynamic_patch=12),
 
684
  reply = response.json()["choices"][0]["message"]["content"]
685
  return reply
686
  except Exception as e:
687
+ return f"Error during model call: {str(e)}"
688
 
689
  if __name__ == "__main__":
690
  parser = argparse.ArgumentParser()
 
696
  args = parser.parse_args()
697
  logger.info(f"args: {args}")
698
 
699
+ # Build main UI and API endpoint interface
700
+ ui_demo = build_demo()
701
+
702
+ api_interface = gr.Interface(
703
+ fn=http_api_infer,
704
+ inputs=[gr.Image(type="pil"), gr.Textbox()],
705
+ outputs="text",
706
+ api_name="/chat"
707
+ )
708
+
709
+ # Serve both under tabs — API tab will be invisible in browser but callable
710
  demo = gr.TabbedInterface(
711
+ interface_list=[ui_demo, api_interface],
712
+ tab_names=["UI", "API"]
713
+ )
714
+
715
+ # ✅ Important: expose API via `api_open=True`
716
+ demo.queue(api_open=True).launch(
717
+ server_name=args.host,
718
+ server_port=args.port,
719
+ share=args.share,
720
+ max_threads=args.concurrency_count,
721
+ )
722
+
723