daqc commited on
Commit
fc2526d
·
verified ·
1 Parent(s): d8ea820

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -64
app.py CHANGED
@@ -633,18 +633,24 @@ User Query: """
633
  # Check whether the user device is a mobile or a computer
634
 
635
  if not request:
636
- return "Unknown device"
637
- # Method 1: Check sec-ch-ua-mobile header
 
638
  is_mobile_header = request.headers.get("sec-ch-ua-mobile")
639
  if is_mobile_header:
640
  return "Mobile" if "?1" in is_mobile_header else "Desktop"
641
 
642
  # Method 2: Check user-agent string
643
  user_agent = request.headers.get("user-agent", "").lower()
644
- mobile_keywords = ["android", "iphone", "ipad", "mobile", "phone"]
645
-
 
646
  if any(keyword in user_agent for keyword in mobile_keywords):
647
  return "Mobile"
 
 
 
 
648
 
649
  # Method 3: Check platform
650
  platform = request.headers.get("sec-ch-ua-platform", "").lower()
@@ -654,11 +660,42 @@ User Query: """
654
  elif platform in ['"windows"', '"macos"', '"linux"']:
655
  return "Desktop"
656
 
 
 
 
 
 
 
 
 
 
657
  # Default case if no clear indicators
658
  return "Desktop"
659
 
660
  def launch(self, **kwargs):
661
- with gr.Blocks(theme="ocean", fill_height=True) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662
  # Different layouts for mobile and computer devices
663
  @gr.render()
664
  def layout(request: gr.Request):
@@ -869,18 +906,19 @@ This AI agent specializes in automated vulnerability research and analysis, buil
869
  [text_input, launch_research_btn],
870
  )
871
 
872
- # Render simple layout
873
  else:
874
- with gr.Blocks(
875
- fill_height=True,
876
- ):
877
- # Project title and repository link at the top
878
- gr.Markdown("""# Open Deep Research Vulnerability Intelligence""")
879
- gr.Markdown("""<img src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" width="20" height="20" style="display: inline-block; vertical-align: middle; margin-right: 8px;"> <a href="https://github.com/mcdaqc/open-deep-research-vulnerability-intelligence" target="_blank">Github Repository</a>""")
880
-
881
- # About section for mobile
882
- with gr.Accordion("ℹ️ About", open=False):
883
- gr.Markdown("""**What it does:**
 
884
  This AI agent specializes in automated vulnerability research and analysis, built on <a href="https://huggingface.co/blog/open-deep-research" target="_blank">Hugging Face's Open Deep Research</a> architecture. It can search across multiple security databases to provide comprehensive vulnerability intelligence reports.
885
 
886
  **Available Tools & APIs:**
@@ -892,7 +930,13 @@ This AI agent specializes in automated vulnerability research and analysis, buil
892
 
893
  **Model Configuration:**
894
  - **Default Model**: Qwen/Qwen2.5-Coder-32B-Instruct (recommended)
895
- - **Alternative**: You can also use Ollama with local models for privacy
 
 
 
 
 
 
896
 
897
  **How to use:**
898
  1. Enter your Hugging Face API key below
@@ -945,44 +989,7 @@ This AI agent specializes in automated vulnerability research and analysis, buil
945
  scale=1,
946
  )
947
 
948
- # Mobile API Key setup event
949
- mobile_setup_api_btn.click(
950
- self.setup_api_key,
951
- [mobile_api_key_input, mobile_max_steps_slider],
952
- [mobile_api_key_status]
953
- )
954
-
955
-
956
-
957
- # Mobile Example button events
958
- mobile_example_btn_1 = gr.Button("🔍 MobaXterm 24.0 vulnerabilities", size="sm", variant="secondary")
959
- mobile_example_btn_2 = gr.Button("🔍 Chrome 120.0.6099.109 security analysis", size="sm", variant="secondary")
960
- mobile_example_btn_3 = gr.Button("🔍 Apache Tomcat 9.0.65 KEV check", size="sm", variant="secondary")
961
-
962
- # Mobile Example button events
963
- mobile_example_btn_1.click(
964
- lambda: "Analyze MobaXterm 24.0 for vulnerabilities as of today",
965
- None,
966
- [text_input]
967
- )
968
- mobile_example_btn_2.click(
969
- lambda: "Check Chrome 120.0.6099.109 for current security issues",
970
- None,
971
- [text_input]
972
- )
973
- mobile_example_btn_3.click(
974
- lambda: "Is Apache Tomcat 9.0.65 in KEV database as of today?",
975
- None,
976
- [text_input]
977
- )
978
-
979
- # Powered by smolagents for mobile
980
- with gr.Row():
981
- gr.HTML("""<div style="display: flex; align-items: center; gap: 8px; font-family: system-ui, -apple-system, sans-serif;">Powered by
982
- <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png" style="width: 32px; height: 32px; object-fit: contain;" alt="logo">
983
- <a target="_blank" href="https://github.com/huggingface/smolagents"><b>hf/smolagents</b></a>
984
- </div>""")
985
-
986
  text_input = gr.Textbox(
987
  lines=1,
988
  label="Your request",
@@ -1001,6 +1008,20 @@ This AI agent specializes in automated vulnerability research and analysis, buil
1001
  mobile_example_btn_2 = gr.Button("🔍 Chrome 120.0.6099.109 security analysis", size="sm", variant="secondary")
1002
  mobile_example_btn_3 = gr.Button("🔍 Apache Tomcat 9.0.65 KEV check", size="sm", variant="secondary")
1003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1004
  # Mobile Example button events
1005
  mobile_example_btn_1.click(
1006
  lambda: "Analyze MobaXterm 24.0 for vulnerabilities as of today",
@@ -1018,13 +1039,7 @@ This AI agent specializes in automated vulnerability research and analysis, buil
1018
  [text_input]
1019
  )
1020
 
1021
- # Powered by smolagents for mobile
1022
- with gr.Row():
1023
- gr.HTML("""<div style="display: flex; align-items: center; gap: 8px; font-family: system-ui, -apple-system, sans-serif;">Powered by
1024
- <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png" style="width: 32px; height: 32px; object-fit: contain;" alt="logo">
1025
- <a target="_blank" href="https://github.com/huggingface/smolagents"><b>hf/smolagents</b></a>
1026
- </div>""")
1027
-
1028
  text_input.submit(
1029
  self.log_user_message,
1030
  [text_input, file_uploads_log],
@@ -1063,10 +1078,62 @@ This AI agent specializes in automated vulnerability research and analysis, buil
1063
  None,
1064
  [text_input, launch_research_btn],
1065
  )
1066
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1067
 
1068
 
1069
- demo.launch(debug=True, **kwargs)
 
 
 
 
 
 
1070
 
1071
  # can this fix ctrl-c no response? no
1072
  try:
 
633
  # Check whether the user device is a mobile or a computer
634
 
635
  if not request:
636
+ return "Desktop" # Default to desktop if no request info
637
+
638
+ # Method 1: Check sec-ch-ua-mobile header (most reliable)
639
  is_mobile_header = request.headers.get("sec-ch-ua-mobile")
640
  if is_mobile_header:
641
  return "Mobile" if "?1" in is_mobile_header else "Desktop"
642
 
643
  # Method 2: Check user-agent string
644
  user_agent = request.headers.get("user-agent", "").lower()
645
+ mobile_keywords = ["android", "iphone", "ipad", "mobile", "phone", "tablet"]
646
+
647
+ # More comprehensive mobile detection
648
  if any(keyword in user_agent for keyword in mobile_keywords):
649
  return "Mobile"
650
+
651
+ # Check for mobile-specific patterns
652
+ if "mobile" in user_agent or "android" in user_agent or "iphone" in user_agent:
653
+ return "Mobile"
654
 
655
  # Method 3: Check platform
656
  platform = request.headers.get("sec-ch-ua-platform", "").lower()
 
660
  elif platform in ['"windows"', '"macos"', '"linux"']:
661
  return "Desktop"
662
 
663
+ # Method 4: Check viewport width (if available)
664
+ viewport_width = request.headers.get("viewport-width")
665
+ if viewport_width:
666
+ try:
667
+ width = int(viewport_width)
668
+ return "Mobile" if width <= 768 else "Desktop"
669
+ except ValueError:
670
+ pass
671
+
672
  # Default case if no clear indicators
673
  return "Desktop"
674
 
675
  def launch(self, **kwargs):
676
+ # Custom CSS for mobile optimization
677
+ custom_css = """
678
+ @media (max-width: 768px) {
679
+ .gradio-container {
680
+ max-width: 100% !important;
681
+ padding: 10px !important;
682
+ }
683
+ .main {
684
+ padding: 10px !important;
685
+ }
686
+ .chatbot {
687
+ max-height: 60vh !important;
688
+ }
689
+ .textbox {
690
+ font-size: 16px !important; /* Prevents zoom on iOS */
691
+ }
692
+ .button {
693
+ min-height: 44px !important; /* Better touch targets */
694
+ }
695
+ }
696
+ """
697
+
698
+ with gr.Blocks(theme="ocean", fill_height=True, css=custom_css) as demo:
699
  # Different layouts for mobile and computer devices
700
  @gr.render()
701
  def layout(request: gr.Request):
 
906
  [text_input, launch_research_btn],
907
  )
908
 
909
+ # Render simple layout for mobile
910
  else:
911
+ try:
912
+ with gr.Blocks(
913
+ fill_height=True,
914
+ ):
915
+ # Project title and repository link at the top
916
+ gr.Markdown("""# Open Deep Research Vulnerability Intelligence""")
917
+ gr.Markdown("""<img src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" width="20" height="20" style="display: inline-block; vertical-align: middle; margin-right: 8px;"> <a href="https://github.com/mcdaqc/open-deep-research-vulnerability-intelligence" target="_blank">Github Repository</a>""")
918
+
919
+ # About section for mobile
920
+ with gr.Accordion("ℹ️ About", open=False):
921
+ gr.Markdown("""**What it does:**
922
  This AI agent specializes in automated vulnerability research and analysis, built on <a href="https://huggingface.co/blog/open-deep-research" target="_blank">Hugging Face's Open Deep Research</a> architecture. It can search across multiple security databases to provide comprehensive vulnerability intelligence reports.
923
 
924
  **Available Tools & APIs:**
 
930
 
931
  **Model Configuration:**
932
  - **Default Model**: Qwen/Qwen2.5-Coder-32B-Instruct (recommended)
933
+ - **Local Models**: For privacy, you can use Ollama with local models:
934
+ 1. Install Ollama: https://ollama.ai/
935
+ 2. Pull a model: `ollama pull qwen2.5-coder:7b`
936
+ 3. Set in your `.env` file:
937
+ - `MODEL_ID=qwen2.5-coder:7b`
938
+ - `OPENAI_API_BASE=http://localhost:11434/v1`
939
+ - `OPENAI_API_KEY=ollama`
940
 
941
  **How to use:**
942
  1. Enter your Hugging Face API key below
 
989
  scale=1,
990
  )
991
 
992
+ # Input section for mobile
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
993
  text_input = gr.Textbox(
994
  lines=1,
995
  label="Your request",
 
1008
  mobile_example_btn_2 = gr.Button("🔍 Chrome 120.0.6099.109 security analysis", size="sm", variant="secondary")
1009
  mobile_example_btn_3 = gr.Button("🔍 Apache Tomcat 9.0.65 KEV check", size="sm", variant="secondary")
1010
 
1011
+ # Powered by smolagents for mobile
1012
+ with gr.Row():
1013
+ gr.HTML("""<div style="display: flex; align-items: center; gap: 8px; font-family: system-ui, -apple-system, sans-serif;">Powered by
1014
+ <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png" style="width: 32px; height: 32px; object-fit: contain;" alt="logo">
1015
+ <a target="_blank" href="https://github.com/huggingface/smolagents"><b>hf/smolagents</b></a>
1016
+ </div>""")
1017
+
1018
+ # Mobile API Key setup event
1019
+ mobile_setup_api_btn.click(
1020
+ self.setup_api_key,
1021
+ [mobile_api_key_input, mobile_max_steps_slider],
1022
+ [mobile_api_key_status]
1023
+ )
1024
+
1025
  # Mobile Example button events
1026
  mobile_example_btn_1.click(
1027
  lambda: "Analyze MobaXterm 24.0 for vulnerabilities as of today",
 
1039
  [text_input]
1040
  )
1041
 
1042
+ # Mobile chat events
 
 
 
 
 
 
1043
  text_input.submit(
1044
  self.log_user_message,
1045
  [text_input, file_uploads_log],
 
1078
  None,
1079
  [text_input, launch_research_btn],
1080
  )
1081
+ except Exception as e:
1082
+ # Fallback to desktop layout if mobile layout fails
1083
+ logger.error(f"Mobile layout failed: {e}")
1084
+ # Re-render desktop layout as fallback
1085
+ with gr.Blocks(fill_height=True):
1086
+ gr.Markdown("""# Open Deep Research Vulnerability Intelligence""")
1087
+ gr.Markdown("""<img src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" width="20" height="20" style="display: inline-block; vertical-align: middle; margin-right: 8px;"> <a href="https://github.com/mcdaqc/open-deep-research-vulnerability-intelligence" target="_blank">Github Repository</a>""")
1088
+ gr.Markdown("⚠️ Mobile layout failed, using desktop layout as fallback.")
1089
+
1090
+ # Simple fallback interface
1091
+ stored_messages = gr.State([])
1092
+ file_uploads_log = gr.State([])
1093
+ chatbot = gr.Chatbot(
1094
+ label="open-Deep-Research",
1095
+ type="messages",
1096
+ avatar_images=(
1097
+ None,
1098
+ "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png",
1099
+ ),
1100
+ )
1101
+
1102
+ text_input = gr.Textbox(
1103
+ lines=1,
1104
+ label="Your request",
1105
+ placeholder="Enter your prompt here and press the button",
1106
+ )
1107
+ launch_research_btn = gr.Button("Run", variant="primary")
1108
+
1109
+ # Fallback events
1110
+ text_input.submit(
1111
+ self.log_user_message,
1112
+ [text_input, file_uploads_log],
1113
+ [stored_messages, text_input, launch_research_btn],
1114
+ ).then(
1115
+ self.interact_with_agent,
1116
+ [stored_messages, chatbot],
1117
+ [chatbot],
1118
+ )
1119
+ launch_research_btn.click(
1120
+ self.log_user_message,
1121
+ [text_input, file_uploads_log],
1122
+ [stored_messages, text_input, launch_research_btn],
1123
+ ).then(
1124
+ self.interact_with_agent,
1125
+ [stored_messages, chatbot],
1126
+ [chatbot],
1127
+ )
1128
 
1129
 
1130
+ demo.launch(
1131
+ debug=True,
1132
+ server_name="0.0.0.0",
1133
+ server_port=7860,
1134
+ share=False,
1135
+ **kwargs
1136
+ )
1137
 
1138
  # can this fix ctrl-c no response? no
1139
  try: