Spaces:
Running
Running
Kovács Ádám
commited on
Commit
·
b13ca35
1
Parent(s):
4dc3a52
Add code to spaces
Browse files- app.py +92 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from lettucedetect.models.inference import HallucinationDetector
|
| 3 |
+
import streamlit.components.v1 as components
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def create_interactive_text(text: str, spans: list[dict[str, int | float]]) -> str:
|
| 7 |
+
"""Create interactive HTML with highlighting and hover effects.
|
| 8 |
+
|
| 9 |
+
:param text: The text to create the interactive text for.
|
| 10 |
+
:param spans: The spans to highlight.
|
| 11 |
+
:return: The interactive text.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
html_text = text
|
| 15 |
+
|
| 16 |
+
for span in sorted(spans, key=lambda x: x["start"], reverse=True):
|
| 17 |
+
span_text = text[span["start"] : span["end"]]
|
| 18 |
+
highlighted_span = f'<span class="hallucination" title="Confidence: {span["confidence"]:.3f}">{span_text}</span>'
|
| 19 |
+
html_text = (
|
| 20 |
+
html_text[: span["start"]] + highlighted_span + html_text[span["end"] :]
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
return f"""
|
| 24 |
+
<style>
|
| 25 |
+
.container {{
|
| 26 |
+
font-family: Arial, sans-serif;
|
| 27 |
+
font-size: 16px;
|
| 28 |
+
line-height: 1.6;
|
| 29 |
+
padding: 20px;
|
| 30 |
+
}}
|
| 31 |
+
.hallucination {{
|
| 32 |
+
background-color: rgba(255, 99, 71, 0.3);
|
| 33 |
+
padding: 2px;
|
| 34 |
+
border-radius: 3px;
|
| 35 |
+
cursor: help;
|
| 36 |
+
}}
|
| 37 |
+
.hallucination:hover {{
|
| 38 |
+
background-color: rgba(255, 99, 71, 0.5);
|
| 39 |
+
}}
|
| 40 |
+
</style>
|
| 41 |
+
<div class="container">{html_text}</div>
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def main():
|
| 46 |
+
st.set_page_config(page_title="Lettuce Detective")
|
| 47 |
+
|
| 48 |
+
st.image(
|
| 49 |
+
"https://github.com/KRLabsOrg/LettuceDetect/blob/main/assets/lettuce_detective.png?raw=true",
|
| 50 |
+
width=600,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
st.title("Let Us Detect Your Hallucinations")
|
| 54 |
+
|
| 55 |
+
@st.cache_resource
|
| 56 |
+
def load_detector():
|
| 57 |
+
return HallucinationDetector(
|
| 58 |
+
method="transformer",
|
| 59 |
+
model_path="KRLabsOrg/lettucedect-base-modernbert-en-v1",
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
detector = load_detector()
|
| 63 |
+
|
| 64 |
+
context = st.text_area(
|
| 65 |
+
"Context",
|
| 66 |
+
"France is a country in Europe. The capital of France is Paris. The population of France is 67 million.",
|
| 67 |
+
height=100,
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
question = st.text_area(
|
| 71 |
+
"Question",
|
| 72 |
+
"What is the capital of France? What is the population of France?",
|
| 73 |
+
height=100,
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
answer = st.text_area(
|
| 77 |
+
"Answer",
|
| 78 |
+
"The capital of France is Paris. The population of France is 69 million.",
|
| 79 |
+
height=100,
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if st.button("Detect Hallucinations"):
|
| 83 |
+
predictions = detector.predict(
|
| 84 |
+
context=[context], question=question, answer=answer, output_format="spans"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
html_content = create_interactive_text(answer, predictions)
|
| 88 |
+
components.html(html_content, height=200)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
lettucedetect
|
| 2 |
+
streamlit
|