Commit
·
f8448b0
0
Parent(s):
Initial commit
Browse files- README.md +45 -0
- app.py +280 -0
- requirements.txt +2 -0
README.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: NullAI
|
| 3 |
+
emoji: 🧠
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
tags:
|
| 12 |
+
- knowledge-reasoning
|
| 13 |
+
- multi-domain
|
| 14 |
+
- expert-verification
|
| 15 |
+
- medical
|
| 16 |
+
- legal
|
| 17 |
+
- deepseek
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
# NullAI - Multi-Domain Knowledge Reasoning System
|
| 21 |
+
|
| 22 |
+
Expert-verified knowledge with transparent verification status.
|
| 23 |
+
|
| 24 |
+
## Features
|
| 25 |
+
|
| 26 |
+
- **55+ Specialized Domains**: Medical, Legal, Programming, Science, and more
|
| 27 |
+
- **Expert Verification**: ORCID-based authentication for expert contributors
|
| 28 |
+
- **Transparent Confidence**: Every response includes confidence scoring
|
| 29 |
+
- **Open Models**: Uses DeepSeek, Qwen, Mistral (no external API required)
|
| 30 |
+
|
| 31 |
+
## Usage
|
| 32 |
+
|
| 33 |
+
1. Select a knowledge domain
|
| 34 |
+
2. Enter your question
|
| 35 |
+
3. View the response with verification status
|
| 36 |
+
|
| 37 |
+
## Verification Marks
|
| 38 |
+
|
| 39 |
+
- 🟢 **Expert Verified**: Reviewed by ORCID-authenticated expert
|
| 40 |
+
- 🔵 **Community Reviewed**: Reviewed by community members
|
| 41 |
+
- ⚪ **Unverified**: Not yet reviewed
|
| 42 |
+
|
| 43 |
+
## License
|
| 44 |
+
|
| 45 |
+
Apache 2.0
|
app.py
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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イ用
|
| 6 |
+
"""
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import os
|
| 9 |
+
import json
|
| 10 |
+
from typing import Optional, List, Dict, Any
|
| 11 |
+
import asyncio
|
| 12 |
+
|
| 13 |
+
# HuggingFace Inference API使用(無料枠あり)
|
| 14 |
+
from huggingface_hub import InferenceClient
|
| 15 |
+
|
| 16 |
+
# 環境変数からトークン取得(オプション)
|
| 17 |
+
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 18 |
+
|
| 19 |
+
# 推論クライアント
|
| 20 |
+
client = InferenceClient(token=HF_TOKEN) if HF_TOKEN else InferenceClient()
|
| 21 |
+
|
| 22 |
+
# ドメイン定義
|
| 23 |
+
DOMAINS = {
|
| 24 |
+
"medical": {"name": "Medical", "model": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", "icon": "🏥"},
|
| 25 |
+
"legal": {"name": "Legal", "model": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", "icon": "⚖️"},
|
| 26 |
+
"economics": {"name": "Economics", "model": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", "icon": "📊"},
|
| 27 |
+
"programming": {"name": "Programming", "model": "Qwen/Qwen2.5-Coder-7B-Instruct", "icon": "💻"},
|
| 28 |
+
"science": {"name": "Science", "model": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", "icon": "🔬"},
|
| 29 |
+
"general": {"name": "General", "model": "mistralai/Mistral-7B-Instruct-v0.3", "icon": "🌐"},
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# 検証マークの状態管理(デモ用)
|
| 33 |
+
verification_store = {}
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def get_system_prompt(domain: str) -> str:
|
| 37 |
+
"""ドメイン固有のシステムプロンプトを生成"""
|
| 38 |
+
prompts = {
|
| 39 |
+
"medical": """You are an expert medical knowledge assistant. Provide accurate, evidence-based medical information.
|
| 40 |
+
Always recommend consulting healthcare professionals for personal medical decisions.
|
| 41 |
+
Include relevant citations when possible.""",
|
| 42 |
+
|
| 43 |
+
"legal": """You are an expert legal knowledge assistant. Provide accurate legal information based on general legal principles.
|
| 44 |
+
Always recommend consulting licensed attorneys for specific legal advice.
|
| 45 |
+
Clarify which jurisdiction the information applies to.""",
|
| 46 |
+
|
| 47 |
+
"economics": """You are an expert economics and finance assistant. Provide accurate economic analysis and financial information.
|
| 48 |
+
Include relevant economic theories and data when applicable.
|
| 49 |
+
Note that this is not financial advice.""",
|
| 50 |
+
|
| 51 |
+
"programming": """You are an expert programming assistant. Provide accurate, well-documented code solutions.
|
| 52 |
+
Follow best practices and explain the reasoning behind your solutions.
|
| 53 |
+
Include error handling and edge cases when relevant.""",
|
| 54 |
+
|
| 55 |
+
"science": """You are an expert science assistant covering physics, chemistry, biology, and related fields.
|
| 56 |
+
Provide accurate scientific explanations with proper terminology.
|
| 57 |
+
Reference established scientific principles and recent research when applicable.""",
|
| 58 |
+
|
| 59 |
+
"general": """You are a helpful knowledge assistant. Provide accurate, well-reasoned answers.
|
| 60 |
+
Be clear about the confidence level of your responses.
|
| 61 |
+
Cite sources when possible."""
|
| 62 |
+
}
|
| 63 |
+
return prompts.get(domain, prompts["general"])
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def generate_response(
|
| 67 |
+
question: str,
|
| 68 |
+
domain: str,
|
| 69 |
+
temperature: float = 0.7,
|
| 70 |
+
max_tokens: int = 1024,
|
| 71 |
+
is_expert: bool = False,
|
| 72 |
+
expert_name: str = ""
|
| 73 |
+
) -> tuple:
|
| 74 |
+
"""
|
| 75 |
+
質問に対する回答を生成
|
| 76 |
+
|
| 77 |
+
Returns:
|
| 78 |
+
(response, thinking, confidence, verification_status)
|
| 79 |
+
"""
|
| 80 |
+
if not question.strip():
|
| 81 |
+
return "Please enter a question.", "", 0.0, "none"
|
| 82 |
+
|
| 83 |
+
domain_info = DOMAINS.get(domain, DOMAINS["general"])
|
| 84 |
+
model_name = domain_info["model"]
|
| 85 |
+
system_prompt = get_system_prompt(domain)
|
| 86 |
+
|
| 87 |
+
# プロンプト構築
|
| 88 |
+
full_prompt = f"""<|system|>
|
| 89 |
+
{system_prompt}
|
| 90 |
+
</s>
|
| 91 |
+
<|user|>
|
| 92 |
+
{question}
|
| 93 |
+
</s>
|
| 94 |
+
<|assistant|>
|
| 95 |
+
Let me think about this step by step.
|
| 96 |
+
|
| 97 |
+
"""
|
| 98 |
+
|
| 99 |
+
try:
|
| 100 |
+
# HuggingFace Inference API呼び出し
|
| 101 |
+
response = client.text_generation(
|
| 102 |
+
full_prompt,
|
| 103 |
+
model=model_name,
|
| 104 |
+
max_new_tokens=max_tokens,
|
| 105 |
+
temperature=temperature,
|
| 106 |
+
do_sample=temperature > 0,
|
| 107 |
+
return_full_text=False
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# 思考プロセスと回答を分離
|
| 111 |
+
thinking = ""
|
| 112 |
+
answer = response
|
| 113 |
+
|
| 114 |
+
if "<thinking>" in response and "</thinking>" in response:
|
| 115 |
+
start = response.find("<thinking>") + len("<thinking>")
|
| 116 |
+
end = response.find("</thinking>")
|
| 117 |
+
thinking = response[start:end].strip()
|
| 118 |
+
answer = response[end + len("</thinking>"):].strip()
|
| 119 |
+
|
| 120 |
+
# 信頼度計算(簡易版)
|
| 121 |
+
confidence = 0.7
|
| 122 |
+
if len(answer) > 200:
|
| 123 |
+
confidence += 0.1
|
| 124 |
+
if "reference" in answer.lower() or "source" in answer.lower():
|
| 125 |
+
confidence += 0.1
|
| 126 |
+
confidence = min(confidence, 0.95)
|
| 127 |
+
|
| 128 |
+
# 検証ステータス
|
| 129 |
+
verification = "none"
|
| 130 |
+
if is_expert and expert_name:
|
| 131 |
+
verification = "expert"
|
| 132 |
+
# 検証情報を保存
|
| 133 |
+
verification_store[hash(question)] = {
|
| 134 |
+
"expert_name": expert_name,
|
| 135 |
+
"verified_at": "now",
|
| 136 |
+
"type": "expert"
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
return answer, thinking, confidence, verification
|
| 140 |
+
|
| 141 |
+
except Exception as e:
|
| 142 |
+
return f"Error: {str(e)}", "", 0.0, "error"
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def format_verification_badge(status: str, expert_name: str = "") -> str:
|
| 146 |
+
"""検証バッジのHTML生成"""
|
| 147 |
+
badges = {
|
| 148 |
+
"expert": f'<span style="background:#4caf50;color:white;padding:2px 8px;border-radius:12px;font-size:12px;">✓ Expert Verified by {expert_name}</span>',
|
| 149 |
+
"community": '<span style="background:#2196f3;color:white;padding:2px 8px;border-radius:12px;font-size:12px;">👥 Community Reviewed</span>',
|
| 150 |
+
"none": '<span style="background:#9e9e9e;color:white;padding:2px 8px;border-radius:12px;font-size:12px;">⚠ Unverified</span>',
|
| 151 |
+
"error": '<span style="background:#f44336;color:white;padding:2px 8px;border-radius:12px;font-size:12px;">❌ Error</span>'
|
| 152 |
+
}
|
| 153 |
+
return badges.get(status, badges["none"])
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
# Gradio Interface
|
| 157 |
+
with gr.Blocks(
|
| 158 |
+
title="NullAI - Multi-Domain Knowledge System",
|
| 159 |
+
theme=gr.themes.Soft(),
|
| 160 |
+
css="""
|
| 161 |
+
.container { max-width: 900px; margin: auto; }
|
| 162 |
+
.badge { display: inline-block; margin: 4px; }
|
| 163 |
+
"""
|
| 164 |
+
) as demo:
|
| 165 |
+
gr.Markdown("""
|
| 166 |
+
# 🧠 NullAI
|
| 167 |
+
### Multi-Domain Knowledge Reasoning System
|
| 168 |
+
|
| 169 |
+
Expert-verified knowledge with transparent verification status.
|
| 170 |
+
Select a domain and ask your question below.
|
| 171 |
+
""")
|
| 172 |
+
|
| 173 |
+
with gr.Row():
|
| 174 |
+
with gr.Column(scale=2):
|
| 175 |
+
domain_dropdown = gr.Dropdown(
|
| 176 |
+
choices=[(f"{v['icon']} {v['name']}", k) for k, v in DOMAINS.items()],
|
| 177 |
+
value="general",
|
| 178 |
+
label="Domain",
|
| 179 |
+
info="Select the knowledge domain"
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
question_input = gr.Textbox(
|
| 183 |
+
label="Your Question",
|
| 184 |
+
placeholder="Enter your question here...",
|
| 185 |
+
lines=3
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 189 |
+
temperature_slider = gr.Slider(
|
| 190 |
+
minimum=0.0,
|
| 191 |
+
maximum=1.0,
|
| 192 |
+
value=0.7,
|
| 193 |
+
step=0.1,
|
| 194 |
+
label="Temperature"
|
| 195 |
+
)
|
| 196 |
+
max_tokens_slider = gr.Slider(
|
| 197 |
+
minimum=256,
|
| 198 |
+
maximum=2048,
|
| 199 |
+
value=1024,
|
| 200 |
+
step=128,
|
| 201 |
+
label="Max Tokens"
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
with gr.Accordion("Expert Verification (Optional)", open=False):
|
| 205 |
+
is_expert_checkbox = gr.Checkbox(
|
| 206 |
+
label="I am a verified expert",
|
| 207 |
+
value=False
|
| 208 |
+
)
|
| 209 |
+
expert_name_input = gr.Textbox(
|
| 210 |
+
label="Expert Name / ORCID",
|
| 211 |
+
placeholder="e.g., Dr. Smith (0000-0001-2345-6789)"
|
| 212 |
+
)
|
| 213 |
+
|
| 214 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
| 215 |
+
|
| 216 |
+
with gr.Column(scale=3):
|
| 217 |
+
verification_html = gr.HTML(
|
| 218 |
+
value=format_verification_badge("none"),
|
| 219 |
+
label="Verification Status"
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
response_output = gr.Textbox(
|
| 223 |
+
label="Response",
|
| 224 |
+
lines=10,
|
| 225 |
+
interactive=False
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
with gr.Accordion("Thinking Process", open=False):
|
| 229 |
+
thinking_output = gr.Textbox(
|
| 230 |
+
label="Model's Reasoning",
|
| 231 |
+
lines=5,
|
| 232 |
+
interactive=False
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
confidence_output = gr.Number(
|
| 236 |
+
label="Confidence Score",
|
| 237 |
+
precision=2
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
# Event handlers
|
| 241 |
+
def process_question(question, domain, temp, max_tok, is_expert, expert_name):
|
| 242 |
+
answer, thinking, confidence, status = generate_response(
|
| 243 |
+
question, domain, temp, max_tok, is_expert, expert_name
|
| 244 |
+
)
|
| 245 |
+
badge_html = format_verification_badge(status, expert_name if is_expert else "")
|
| 246 |
+
return answer, thinking, confidence, badge_html
|
| 247 |
+
|
| 248 |
+
submit_btn.click(
|
| 249 |
+
fn=process_question,
|
| 250 |
+
inputs=[
|
| 251 |
+
question_input,
|
| 252 |
+
domain_dropdown,
|
| 253 |
+
temperature_slider,
|
| 254 |
+
max_tokens_slider,
|
| 255 |
+
is_expert_checkbox,
|
| 256 |
+
expert_name_input
|
| 257 |
+
],
|
| 258 |
+
outputs=[
|
| 259 |
+
response_output,
|
| 260 |
+
thinking_output,
|
| 261 |
+
confidence_output,
|
| 262 |
+
verification_html
|
| 263 |
+
]
|
| 264 |
+
)
|
| 265 |
+
|
| 266 |
+
gr.Markdown("""
|
| 267 |
+
---
|
| 268 |
+
### About NullAI
|
| 269 |
+
|
| 270 |
+
NullAI is a multi-domain knowledge reasoning system with:
|
| 271 |
+
- **55+ specialized domains** (medical, legal, programming, etc.)
|
| 272 |
+
- **Expert verification** via ORCID authentication
|
| 273 |
+
- **Transparent confidence scores** for all responses
|
| 274 |
+
- **Open-source models** (no external API dependencies)
|
| 275 |
+
|
| 276 |
+
[GitHub](https://github.com/your-repo) | [Documentation](https://your-docs-url)
|
| 277 |
+
""")
|
| 278 |
+
|
| 279 |
+
if __name__ == "__main__":
|
| 280 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
huggingface_hub>=0.20.0
|