openfree commited on
Commit
5eb62f9
·
verified ·
1 Parent(s): b5f1109

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -96
app.py CHANGED
@@ -125,22 +125,20 @@ def do_web_search(query: str) -> str:
125
  summary_lines.append(
126
  f"### Result {idx}: {title}\n\n"
127
  f"{snippet}\n\n"
128
- f"**출처**: [{displayed_link}]({link})\n\n"
129
  f"---\n"
130
  )
131
 
132
  # 모델에게 명확한 지침 추가
133
  instructions = """
134
- # 웹 검색 결과
135
- 아래는 검색 결과입니다. 질문에 답변할 때 이 정보를 활용하세요:
136
- 1. 각 결과의 제목, 내용, 출처 링크를 참고하세요
137
- 2. 답변에 관련 정보의 출처를 명시적으로 인용하세요 (예: "X 출처에 따르면...")
138
- 3. 응답에 실제 출처 링크를 포함하세요
139
- 4. 여러 출처의 정보를 종합하여 답변하세요
140
  """
141
 
142
  search_results = instructions + "\n".join(summary_lines)
143
- logger.info(f"검색 결과 {len(limited_organic)}개 처리 완료")
144
  return search_results
145
 
146
  except Exception as e:
@@ -500,13 +498,12 @@ def run(
500
  ws_result = do_web_search(ws_query)
501
  combined_system_msg += f"[Search top-20 Full Items Based on user prompt]\n{ws_result}\n\n"
502
  # >>> 추가된 안내 문구 (검색 결과의 link 등 출처를 활용)
503
- combined_system_msg += "[참고: 위 검색결과 내용과 link를 출처로 인용하여 답변해 주세요.]\n\n"
504
  combined_system_msg += """
505
- [중요 지시사항]
506
- 1. 답변에 검색 결과에서 찾은 정보의 출처를 반드시 인용하세요.
507
- 2. 출처 인용 시 "[출처 제목](링크)" 형식의 마크다운 링크를 사용하세요.
508
- 3. 여러 출처의 정보를 종합하여 답변하세요.
509
- 4. 답변 마지막에 "참고 자료:" 섹션을 추가하고 사용한 주요 출처 링크를 나열하세요.
510
  """
511
  else:
512
  combined_system_msg += "[No valid keywords found, skipping WebSearch]\n\n"
@@ -544,7 +541,7 @@ def run(
544
 
545
  streamer = TextIteratorStreamer(processor, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
546
  gen_kwargs = dict(
547
- inputs,
548
  streamer=streamer,
549
  max_new_tokens=max_new_tokens,
550
  )
@@ -559,7 +556,7 @@ def run(
559
 
560
  except Exception as e:
561
  logger.error(f"Error in run: {str(e)}")
562
- yield f"죄송합니다. 오류가 발생했습니다: {str(e)}"
563
 
564
  finally:
565
  # 임시 파일 삭제
@@ -581,12 +578,12 @@ def run(
581
 
582
 
583
  ##############################################################################
584
- # 예시들 (한글화)
585
  ##############################################################################
586
  examples = [
587
  [
588
  {
589
- "text": "두 PDF 파일 내용을 비교하라.",
590
  "files": [
591
  "assets/additional-examples/before.pdf",
592
  "assets/additional-examples/after.pdf",
@@ -595,43 +592,43 @@ examples = [
595
  ],
596
  [
597
  {
598
- "text": "CSV 파일 내용을 요약, 분석하라",
599
  "files": ["assets/additional-examples/sample-csv.csv"],
600
  }
601
  ],
602
  [
603
  {
604
- "text": "너는 친근하고 다정한 이해심 많은 여자친구 역할이다. 이 비디오에 대해 설명해줘줘",
605
  "files": ["assets/additional-examples/tmp.mp4"],
606
  }
607
  ],
608
  [
609
  {
610
- "text": "표지 내용을 설명하고 글자를 읽어주세요.",
611
  "files": ["assets/additional-examples/maz.jpg"],
612
  }
613
  ],
614
  [
615
  {
616
- "text": "이미 이 영양제를 <image> 가지고 있고, 이 제품 <image>을 새로 사려 합니다. 함께 섭취할 때 주의해야 할 점이 있을까요?",
617
  "files": ["assets/additional-examples/pill1.png", "assets/additional-examples/pill2.png"],
618
  }
619
  ],
620
  [
621
  {
622
- "text": "이 적분을 풀어주세요.",
623
  "files": ["assets/additional-examples/4.png"],
624
  }
625
  ],
626
  [
627
  {
628
- "text": "이 티켓은 언제 발급된 것이고, 가격은 얼마인가요?",
629
  "files": ["assets/additional-examples/2.png"],
630
  }
631
  ],
632
  [
633
  {
634
- "text": "이미지들의 순서를 바탕으로 짧은 이야기를 만들어 주세요.",
635
  "files": [
636
  "assets/sample-images/09-1.png",
637
  "assets/sample-images/09-2.png",
@@ -641,32 +638,30 @@ examples = [
641
  ],
642
  }
643
  ],
644
-
645
  [
646
  {
647
- "text": "동일한 막대 그래프를 그리는 matplotlib 코드를 작성해주세요.",
648
  "files": ["assets/additional-examples/barchart.png"],
649
  }
650
  ],
651
  [
652
  {
653
- "text": "이미지에 있는 텍스트를 그대로 읽어서 마크다운 형태로 적어주세요.",
654
  "files": ["assets/additional-examples/3.png"],
655
  }
656
  ],
657
  [
658
  {
659
- "text": "이 표지판에는 무슨 문구가 적혀 있나요?",
660
  "files": ["assets/sample-images/02.png"],
661
  }
662
  ],
663
  [
664
  {
665
- "text": "두 이미지를 비교해서 공통점과 차이점을 말해주세요.",
666
  "files": ["assets/sample-images/03.png"],
667
  }
668
  ],
669
-
670
  ]
671
 
672
  ##############################################################################
@@ -772,80 +767,73 @@ title_html = """
772
  <h1 align="center" style="margin-bottom: 0.2em; font-size: 1.6em;"> 🤗 Gemma3-R1984-27B </h1>
773
  <p align="center" style="font-size:1.1em; color:#555;">
774
  ✅Agentic AI Platform ✅Reasoning & Uncensored ✅Multimodal & VLM ✅Deep-Research & RAG <br>
775
- Operates on an ✅'NVIDIA A100 GPU' as an independent local server, enhancing security and preventing information leakage.<br>
776
  @Based by 'MS Gemma-3-27b' / @Powered by 'MOUSE-II'(VIDRAFT)
777
  </p>
778
  """
779
 
 
 
 
 
 
 
780
 
 
 
 
 
 
 
 
 
781
 
782
- with gr.Blocks(css=css, title="Gemma3-R1984-27B") as demo:
783
- gr.Markdown(title_html)
 
 
784
 
785
- # 웹서치 옵션은 화면에 표시 (하지만 시스템 프롬프트, 토큰 슬라이더 등은 감춤)
786
- web_search_checkbox = gr.Checkbox(
787
- label="Deep Research",
788
- value=False
789
- )
 
790
 
791
- # 내부적으로 쓰이지만 화면에는 노출되지 않도록 설정
792
- system_prompt_box = gr.Textbox(
793
- lines=3,
794
- value="반드시 한글로 답변하라. You are a deep thinking AI, you may use extremely long chains of thought to deeply consider the problem and deliberate with yourself via systematic reasoning processes to help come to a correct solution prior to answering. Please answer in Korean.You have the ability to read English sources, but you **must always speak in Korean**.Even if the search results are in English, answer in Korean.",
795
- visible=False # 화면에서 감춤
796
- )
797
-
798
- max_tokens_slider = gr.Slider(
799
- label="Max New Tokens",
800
- minimum=100,
801
- maximum=8000,
802
- step=50,
803
- value=1000,
804
- visible=False # 화면에서 감춤
805
- )
806
-
807
- web_search_text = gr.Textbox(
808
- lines=1,
809
- label="(Unused) Web Search Query",
810
- placeholder="No direct input needed",
811
- visible=False # 화면에서 감춤
812
- )
813
-
814
- # 채팅 인터페이스 구성
815
- chat = gr.ChatInterface(
816
- fn=run,
817
- type="messages",
818
- chatbot=gr.Chatbot(type="messages", scale=1, allow_tags=["image"]),
819
- textbox=gr.MultimodalTextbox(
820
- file_types=[
821
- ".webp", ".png", ".jpg", ".jpeg", ".gif",
822
- ".mp4", ".csv", ".txt", ".pdf"
823
- ],
824
- file_count="multiple",
825
- autofocus=True
826
- ),
827
- multimodal=True,
828
- additional_inputs=[
829
- system_prompt_box,
830
- max_tokens_slider,
831
- web_search_checkbox,
832
- web_search_text,
833
  ],
834
- stop_btn=False,
835
- title='<a href="https://discord.gg/openfreeai" target="_blank">https://discord.gg/openfreeai</a>',
836
- examples=examples,
837
- run_examples_on_click=False,
838
- cache_examples=False,
839
- css_paths=None,
840
- delete_cache=(1800, 1800),
841
- )
842
-
843
- # 예제 섹션 - 이미 ChatInterface에 examples가 설정되어 있으므로 여기서는 설명만 표시
844
- with gr.Row(elem_id="examples_row"):
845
- with gr.Column(scale=12, elem_id="examples_container"):
846
- gr.Markdown("### Example Inputs (click to load)")
 
 
 
 
 
847
 
 
 
 
 
848
 
849
  if __name__ == "__main__":
850
- # 로컬에서만 실행 시
851
- demo.launch()
 
125
  summary_lines.append(
126
  f"### Result {idx}: {title}\n\n"
127
  f"{snippet}\n\n"
128
+ f"**Source**: [{displayed_link}]({link})\n\n"
129
  f"---\n"
130
  )
131
 
132
  # 모델에게 명확한 지침 추가
133
  instructions = """
134
+ # Web Search Results
135
+ Below are the search results. Please refer to the title, snippet, and source link of each result when answering:
136
+ 1. Cite the sources explicitly in your answer (e.g., "According to [Source Title](link)...").
137
+ 2. Incorporate information from multiple sources.
 
 
138
  """
139
 
140
  search_results = instructions + "\n".join(summary_lines)
141
+ logger.info(f"Processed {len(limited_organic)} search results")
142
  return search_results
143
 
144
  except Exception as e:
 
498
  ws_result = do_web_search(ws_query)
499
  combined_system_msg += f"[Search top-20 Full Items Based on user prompt]\n{ws_result}\n\n"
500
  # >>> 추가된 안내 문구 (검색 결과의 link 등 출처를 활용)
501
+ combined_system_msg += "[Note: Use the above search results and their links as sources when answering.]\n\n"
502
  combined_system_msg += """
503
+ [Important Instructions]
504
+ 1. Cite the sources found in the search results using markdown links, e.g., "[Source Title](link)".
505
+ 2. Combine information from multiple sources in your answer.
506
+ 3. At the end of your answer, add a "References:" section listing the key source links.
 
507
  """
508
  else:
509
  combined_system_msg += "[No valid keywords found, skipping WebSearch]\n\n"
 
541
 
542
  streamer = TextIteratorStreamer(processor, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
543
  gen_kwargs = dict(
544
+ inputs=inputs,
545
  streamer=streamer,
546
  max_new_tokens=max_new_tokens,
547
  )
 
556
 
557
  except Exception as e:
558
  logger.error(f"Error in run: {str(e)}")
559
+ yield f"Sorry, an error occurred: {str(e)}"
560
 
561
  finally:
562
  # 임시 파일 삭제
 
578
 
579
 
580
  ##############################################################################
581
+ # 예시들 (모두 영어로)
582
  ##############################################################################
583
  examples = [
584
  [
585
  {
586
+ "text": "Compare the contents of the two PDF files.",
587
  "files": [
588
  "assets/additional-examples/before.pdf",
589
  "assets/additional-examples/after.pdf",
 
592
  ],
593
  [
594
  {
595
+ "text": "Summarize and analyze the contents of the CSV file.",
596
  "files": ["assets/additional-examples/sample-csv.csv"],
597
  }
598
  ],
599
  [
600
  {
601
+ "text": "Assume the role of a friendly and understanding girlfriend. Describe this video.",
602
  "files": ["assets/additional-examples/tmp.mp4"],
603
  }
604
  ],
605
  [
606
  {
607
+ "text": "Describe the cover and read the text on it.",
608
  "files": ["assets/additional-examples/maz.jpg"],
609
  }
610
  ],
611
  [
612
  {
613
+ "text": "I already have this supplement <image> and I plan to buy this product <image>. Are there any precautions when taking them together?",
614
  "files": ["assets/additional-examples/pill1.png", "assets/additional-examples/pill2.png"],
615
  }
616
  ],
617
  [
618
  {
619
+ "text": "Solve this integral.",
620
  "files": ["assets/additional-examples/4.png"],
621
  }
622
  ],
623
  [
624
  {
625
+ "text": "When was this ticket issued, and what is its price?",
626
  "files": ["assets/additional-examples/2.png"],
627
  }
628
  ],
629
  [
630
  {
631
+ "text": "Based on the sequence of these images, create a short story.",
632
  "files": [
633
  "assets/sample-images/09-1.png",
634
  "assets/sample-images/09-2.png",
 
638
  ],
639
  }
640
  ],
 
641
  [
642
  {
643
+ "text": "Write Python code using matplotlib to plot a bar chart that matches this image.",
644
  "files": ["assets/additional-examples/barchart.png"],
645
  }
646
  ],
647
  [
648
  {
649
+ "text": "Read the text in the image and write it out in Markdown format.",
650
  "files": ["assets/additional-examples/3.png"],
651
  }
652
  ],
653
  [
654
  {
655
+ "text": "What does this sign say?",
656
  "files": ["assets/sample-images/02.png"],
657
  }
658
  ],
659
  [
660
  {
661
+ "text": "Compare the two images and describe their similarities and differences.",
662
  "files": ["assets/sample-images/03.png"],
663
  }
664
  ],
 
665
  ]
666
 
667
  ##############################################################################
 
767
  <h1 align="center" style="margin-bottom: 0.2em; font-size: 1.6em;"> 🤗 Gemma3-R1984-27B </h1>
768
  <p align="center" style="font-size:1.1em; color:#555;">
769
  ✅Agentic AI Platform ✅Reasoning & Uncensored ✅Multimodal & VLM ✅Deep-Research & RAG <br>
770
+ Operates on an NVIDIA A100 GPU as an independent local server, enhancing security and preventing information leakage.<br>
771
  @Based by 'MS Gemma-3-27b' / @Powered by 'MOUSE-II'(VIDRAFT)
772
  </p>
773
  """
774
 
775
+ # 수정된 시스템 프롬프트: 입력 언어에 맞춰 응답하도록 지시
776
+ system_prompt_box = gr.Textbox(
777
+ lines=3,
778
+ value="You are a deep thinking AI. Please respond in the same language as the input prompt. If the input is in English, answer in English. If it is in another language (e.g., Korean, Japanese), answer in that language using your full reasoning capabilities.",
779
+ visible=False # 화면에서 감춤
780
+ )
781
 
782
+ max_tokens_slider = gr.Slider(
783
+ label="Max New Tokens",
784
+ minimum=100,
785
+ maximum=8000,
786
+ step=50,
787
+ value=1000,
788
+ visible=False # 화면에서 감춤
789
+ )
790
 
791
+ web_search_checkbox = gr.Checkbox(
792
+ label="Deep Research",
793
+ value=False
794
+ )
795
 
796
+ web_search_text = gr.Textbox(
797
+ lines=1,
798
+ label="(Unused) Web Search Query",
799
+ placeholder="No direct input needed",
800
+ visible=False # 화면에서 감춤
801
+ )
802
 
803
+ # 채팅 인터페이스 구성
804
+ chat = gr.ChatInterface(
805
+ fn=run,
806
+ type="messages",
807
+ chatbot=gr.Chatbot(type="messages", scale=1, allow_tags=["image"]),
808
+ textbox=gr.MultimodalTextbox(
809
+ file_types=[
810
+ ".webp", ".png", ".jpg", ".jpeg", ".gif",
811
+ ".mp4", ".csv", ".txt", ".pdf"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
812
  ],
813
+ file_count="multiple",
814
+ autofocus=True
815
+ ),
816
+ multimodal=True,
817
+ additional_inputs=[
818
+ system_prompt_box,
819
+ max_tokens_slider,
820
+ web_search_checkbox,
821
+ web_search_text,
822
+ ],
823
+ stop_btn=False,
824
+ title='<a href="https://discord.gg/openfreeai" target="_blank">https://discord.gg/openfreeai</a>',
825
+ examples=examples,
826
+ run_examples_on_click=False,
827
+ cache_examples=False,
828
+ css_paths=None,
829
+ delete_cache=(1800, 1800),
830
+ )
831
 
832
+ # 예제 섹션 - 이미 ChatInterface에 examples가 설정되어 있으므로 여기서는 설명만 표시
833
+ with gr.Row(elem_id="examples_row"):
834
+ with gr.Column(scale=12, elem_id="examples_container"):
835
+ gr.Markdown("### Example Inputs (click to load)")
836
 
837
  if __name__ == "__main__":
838
+ # Run locally
839
+ chat.launch()