kimappl commited on
Commit
6608bce
·
verified ·
1 Parent(s): d2e26b4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import time
4
+
5
+ # Page configuration
6
+ st.set_page_config(page_title="AI Q&A Assistant", layout="wide")
7
+
8
+ # Custom styling
9
+ st.markdown("""
10
+ <style>
11
+ .header {
12
+ background-color: #1565C0;
13
+ padding: 60px;
14
+ text-align: center;
15
+ border-radius: 15px;
16
+ color: white;
17
+ font-family: 'Arial', sans-serif;
18
+ }
19
+ .header h1 {
20
+ font-size: 50px;
21
+ font-weight: bold;
22
+ }
23
+ .qa-box {
24
+ background-color: #f5f5f5;
25
+ padding: 20px;
26
+ border-radius: 10px;
27
+ margin: 10px 0;
28
+ }
29
+ </style>
30
+ <div class="header">
31
+ <h1>🤔 AI Q&A Assistant</h1>
32
+ <p>Get answers to your questions from any text</p>
33
+ </div>
34
+ """, unsafe_allow_html=True)
35
+
36
+ # Load model
37
+ @st.cache_resource
38
+ def load_qa_model():
39
+ return pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
40
+
41
+ qa_model = load_qa_model()
42
+
43
+ # Main content
44
+ col1, col2 = st.columns([6, 4])
45
+
46
+ with col1:
47
+ st.subheader("Context")
48
+ context = st.text_area(
49
+ "Enter the text passage:",
50
+ height=300,
51
+ help="This is the text that the AI will use to answer questions"
52
+ )
53
+
54
+ with col2:
55
+ st.subheader("Question")
56
+ question = st.text_input(
57
+ "Ask a question about the text:",
58
+ help="Make sure your question can be answered using the provided text"
59
+ )
60
+
61
+ # History tracking
62
+ if 'qa_history' not in st.session_state:
63
+ st.session_state.qa_history = []
64
+
65
+ if qa_model and st.button("Get Answer"):
66
+ if context and question:
67
+ with st.spinner("Finding answer..."):
68
+ start_time = time.time()
69
+
70
+ result = qa_model(
71
+ question=question,
72
+ context=context
73
+ )
74
+
75
+ end_time = time.time()
76
+
77
+ # Add to history
78
+ st.session_state.qa_history.append({
79
+ 'question': question,
80
+ 'answer': result['answer'],
81
+ 'confidence': result['score']
82
+ })
83
+
84
+ # Display result
85
+ st.markdown(f"""
86
+ <div class="qa-box">
87
+ <h3>Answer:</h3>
88
+ <p>{result['answer']}</p>
89
+ <small>Confidence: {result['score']:.2%}</small><br>
90
+ <small>Response time: {(end_time - start_time):.2f} seconds</small>
91
+ </div>
92
+ """, unsafe_allow_html=True)
93
+ else:
94
+ st.warning("Please provide both context and a question")
95
+
96
+ # Display history
97
+ if st.session_state.qa_history:
98
+ st.subheader("Question History")
99
+ for i, qa in enumerate(reversed(st.session_state.qa_history[-5:])):
100
+ st.markdown(f"""
101
+ <div class="qa-box">
102
+ <strong>Q: {qa['question']}</strong><br>
103
+ A: {qa['answer']}<br>
104
+ <small>Confidence: {qa['confidence']:.2%}</small>
105
+ </div>
106
+ """, unsafe_allow_html=True)
107
+
108
+ # Clear history button
109
+ if st.session_state.qa_history:
110
+ if st.button("Clear History"):
111
+ st.session_state.qa_history = []
112
+ st.experimental_rerun()