Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- .gitattributes +1 -0
- dockerfile +0 -0
- medquad_full.json +3 -0
- requirements.txt +8 -0
- templates/terminal_chat.html +125 -0
- web_terminal_chat.py +60 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
medquad_full.json filter=lfs diff=lfs merge=lfs -text
|
dockerfile
ADDED
|
File without changes
|
medquad_full.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0cde0872638b218d30a48cd991ec1d70c3f2a1316fcb8f7bcf93b128eb49c0fe
|
| 3 |
+
size 23093158
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
jinja2
|
| 4 |
+
torch
|
| 5 |
+
scikit-learn
|
| 6 |
+
numpy
|
| 7 |
+
python-multipart
|
| 8 |
+
sentence-transformers
|
templates/terminal_chat.html
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<title>MedQA Terminal Chatbot</title>
|
| 6 |
+
<style>
|
| 7 |
+
body {
|
| 8 |
+
background: #181818;
|
| 9 |
+
color: #e0e0e0;
|
| 10 |
+
font-family: 'Fira Mono', 'Consolas', monospace;
|
| 11 |
+
margin: 0;
|
| 12 |
+
padding: 0;
|
| 13 |
+
}
|
| 14 |
+
.container {
|
| 15 |
+
display: flex;
|
| 16 |
+
height: 100vh;
|
| 17 |
+
}
|
| 18 |
+
.chat {
|
| 19 |
+
flex: 2;
|
| 20 |
+
border-right: 2px solid #333;
|
| 21 |
+
padding: 32px 24px 24px 24px;
|
| 22 |
+
display: flex;
|
| 23 |
+
flex-direction: column;
|
| 24 |
+
}
|
| 25 |
+
.chat-history {
|
| 26 |
+
flex: 1;
|
| 27 |
+
overflow-y: auto;
|
| 28 |
+
margin-bottom: 16px;
|
| 29 |
+
}
|
| 30 |
+
.chat-entry {
|
| 31 |
+
margin-bottom: 18px;
|
| 32 |
+
}
|
| 33 |
+
.user {
|
| 34 |
+
color: #8be9fd;
|
| 35 |
+
}
|
| 36 |
+
.bot {
|
| 37 |
+
color: #50fa7b;
|
| 38 |
+
}
|
| 39 |
+
.confidence {
|
| 40 |
+
color: #ffb86c;
|
| 41 |
+
font-size: 0.95em;
|
| 42 |
+
}
|
| 43 |
+
.matched-q {
|
| 44 |
+
color: #bfbfbf;
|
| 45 |
+
font-size: 0.92em;
|
| 46 |
+
margin-left: 12px;
|
| 47 |
+
}
|
| 48 |
+
.input-form {
|
| 49 |
+
display: flex;
|
| 50 |
+
gap: 8px;
|
| 51 |
+
}
|
| 52 |
+
.input-form input[type="text"] {
|
| 53 |
+
flex: 1;
|
| 54 |
+
background: #222;
|
| 55 |
+
color: #e0e0e0;
|
| 56 |
+
border: 1px solid #444;
|
| 57 |
+
border-radius: 4px;
|
| 58 |
+
padding: 8px 12px;
|
| 59 |
+
font-size: 1em;
|
| 60 |
+
}
|
| 61 |
+
.input-form button {
|
| 62 |
+
background: #444;
|
| 63 |
+
color: #fff;
|
| 64 |
+
border: none;
|
| 65 |
+
border-radius: 4px;
|
| 66 |
+
padding: 8px 18px;
|
| 67 |
+
font-family: inherit;
|
| 68 |
+
font-size: 1em;
|
| 69 |
+
cursor: pointer;
|
| 70 |
+
}
|
| 71 |
+
.input-form button:hover {
|
| 72 |
+
background: #666;
|
| 73 |
+
}
|
| 74 |
+
.info {
|
| 75 |
+
flex: 1;
|
| 76 |
+
padding: 32px 24px 24px 24px;
|
| 77 |
+
background: #1a1a1a;
|
| 78 |
+
}
|
| 79 |
+
.info h2 {
|
| 80 |
+
color: #ff79c6;
|
| 81 |
+
font-size: 1.2em;
|
| 82 |
+
margin-top: 0;
|
| 83 |
+
}
|
| 84 |
+
.info pre {
|
| 85 |
+
background: #222;
|
| 86 |
+
color: #e0e0e0;
|
| 87 |
+
padding: 12px;
|
| 88 |
+
border-radius: 6px;
|
| 89 |
+
font-size: 0.98em;
|
| 90 |
+
overflow-x: auto;
|
| 91 |
+
}
|
| 92 |
+
</style>
|
| 93 |
+
</head>
|
| 94 |
+
<body>
|
| 95 |
+
<div class="container">
|
| 96 |
+
<div class="chat">
|
| 97 |
+
<div class="chat-history">
|
| 98 |
+
{% for user, bot, conf, matched_q in chat_history %}
|
| 99 |
+
<div class="chat-entry">
|
| 100 |
+
<span class="user">You:</span> {{ user }}<br>
|
| 101 |
+
<span class="bot">Bot:</span> {{ bot }} <span class="confidence">(similarity: {{ '%.2f'|format(conf) }})</span><br>
|
| 102 |
+
<span class="matched-q">Matched Q: {{ matched_q }}</span>
|
| 103 |
+
</div>
|
| 104 |
+
{% else %}
|
| 105 |
+
<div class="chat-entry bot">Type your medical question below.</div>
|
| 106 |
+
{% endfor %}
|
| 107 |
+
</div>
|
| 108 |
+
<form class="input-form" action="/chat" method="post" autocomplete="off">
|
| 109 |
+
<input type="text" name="user_input" placeholder="Ask a medical question..." required autofocus>
|
| 110 |
+
<button type="submit">Send</button>
|
| 111 |
+
</form>
|
| 112 |
+
</div>
|
| 113 |
+
<div class="info">
|
| 114 |
+
<h2>Info</h2>
|
| 115 |
+
<pre>
|
| 116 |
+
Model: SentenceTransformer (all-MiniLM-L6-v2)
|
| 117 |
+
Retrieval: Cosine Similarity
|
| 118 |
+
Data: medquad_full.json
|
| 119 |
+
|
| 120 |
+
Disclaimer: This is a demo. For real medical advice, consult a professional.
|
| 121 |
+
</pre>
|
| 122 |
+
</div>
|
| 123 |
+
</div>
|
| 124 |
+
</body>
|
| 125 |
+
</html>
|
web_terminal_chat.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import uvicorn
|
| 2 |
+
from fastapi import FastAPI, Request, Form
|
| 3 |
+
from fastapi.responses import HTMLResponse, RedirectResponse
|
| 4 |
+
from fastapi.templating import Jinja2Templates
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
app = FastAPI()
|
| 12 |
+
|
| 13 |
+
# Templates
|
| 14 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 15 |
+
templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "templates"))
|
| 16 |
+
|
| 17 |
+
# Chatverlauf im Speicher (nur für Demo, nicht persistent)
|
| 18 |
+
chat_history = []
|
| 19 |
+
last_confidence = None
|
| 20 |
+
last_matched_question = None
|
| 21 |
+
|
| 22 |
+
# Embedding-Modell und Daten laden
|
| 23 |
+
with open(os.path.join(BASE_DIR, 'medquad_full.json'), 'r', encoding='utf-8') as f:
|
| 24 |
+
faq_data = json.load(f)
|
| 25 |
+
questions = [item['question'] for item in faq_data]
|
| 26 |
+
answers = [item['answer'] for item in faq_data]
|
| 27 |
+
|
| 28 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 29 |
+
question_embeddings = model.encode(questions, show_progress_bar=True, convert_to_numpy=True)
|
| 30 |
+
|
| 31 |
+
def retrieve_answer(user_question):
|
| 32 |
+
user_emb = model.encode([user_question], convert_to_numpy=True)
|
| 33 |
+
sims = cosine_similarity(user_emb, question_embeddings)[0]
|
| 34 |
+
best_idx = int(np.argmax(sims))
|
| 35 |
+
best_score = float(sims[best_idx])
|
| 36 |
+
best_question = questions[best_idx]
|
| 37 |
+
answer = answers[best_idx]
|
| 38 |
+
return answer, best_score, best_question
|
| 39 |
+
|
| 40 |
+
@app.get("/", response_class=HTMLResponse)
|
| 41 |
+
def index(request: Request):
|
| 42 |
+
return templates.TemplateResponse("terminal_chat.html", {
|
| 43 |
+
"request": request,
|
| 44 |
+
"chat_history": chat_history,
|
| 45 |
+
"accuracy": None, # Nicht mehr relevant
|
| 46 |
+
"last_confidence": last_confidence,
|
| 47 |
+
"last_matched_question": last_matched_question,
|
| 48 |
+
})
|
| 49 |
+
|
| 50 |
+
@app.post("/chat", response_class=HTMLResponse)
|
| 51 |
+
def chat(request: Request, user_input: str = Form(...)):
|
| 52 |
+
global last_confidence, last_matched_question
|
| 53 |
+
answer, conf, matched_q = retrieve_answer(user_input)
|
| 54 |
+
chat_history.append((user_input, answer, conf, matched_q))
|
| 55 |
+
last_confidence = conf
|
| 56 |
+
last_matched_question = matched_q
|
| 57 |
+
return RedirectResponse("/", status_code=303)
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
uvicorn.run("web_terminal_chat:app", host="127.0.0.1", port=8000, reload=True)
|