Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
⚖️ Title: 🤖 eRAG-Election
|
| 3 |
+
License: Apache-2.0
|
| 4 |
+
|
| 5 |
+
This project is licensed under the Apache License, Version 2.0 (the "License");
|
| 6 |
+
you may not use this file except in compliance with the License.
|
| 7 |
+
You may obtain a copy of the License at
|
| 8 |
+
|
| 9 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 10 |
+
|
| 11 |
+
Unless required by applicable law or agreed to in writing, software
|
| 12 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 13 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 14 |
+
See the License for the specific language governing permissions and
|
| 15 |
+
limitations under the License.
|
| 16 |
+
|
| 17 |
+
For inquiries or contributions, please contact:
|
| 18 |
+
amornpan@gmail.com
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
import streamlit as st
|
| 22 |
+
import requests
|
| 23 |
+
import os
|
| 24 |
+
|
| 25 |
+
st.title('eRAG-Election-v1')
|
| 26 |
+
|
| 27 |
+
st.markdown("""
|
| 28 |
+
##### 🤖 AI เพื่อการปรับปรุงงาน กกต.
|
| 29 |
+
ระบบ AI นี้ถูกออกแบบมาเพื่อช่วยให้คณะกรรมการการเลือกตั้ง (กกต.) เข้าถึงข้อมูลและการวิเคราะห์ความหมายในเชิงบริบทได้อย่างลึกซึ้ง
|
| 30 |
+
ทำให้สามารถเข้าใจความเชื่อมโยงและความหมายของข้อมูลในสถานการณ์การเลือกตั้งได้อย่างแม่นยำ
|
| 31 |
+
ระบบได้รับการออกแบบเพื่อรองรับการเรียนรู้เชิงลึกและการประมวลผลข้อมูลเกี่ยวกับการเลือกตั้งและการจัดการข้อมูลผู้มีสิทธิเลือกตั้ง
|
| 32 |
+
""", unsafe_allow_html=True)
|
| 33 |
+
|
| 34 |
+
def format_file_size(size_in_bytes):
|
| 35 |
+
for unit in ['B', 'KB', 'MB', 'GB']:
|
| 36 |
+
if size_in_bytes < 1024:
|
| 37 |
+
return f"{size_in_bytes:.2f} {unit}"
|
| 38 |
+
size_in_bytes /= 1024
|
| 39 |
+
return f"{size_in_bytes:.2f} GB"
|
| 40 |
+
|
| 41 |
+
def display_search_result(result, index):
|
| 42 |
+
with st.expander(f"🔍 Search Result #{index + 1} (Score: {result['score']:.4f})"):
|
| 43 |
+
st.markdown("#### 📄 Document Information")
|
| 44 |
+
col1, col2 = st.columns(2)
|
| 45 |
+
|
| 46 |
+
with col1:
|
| 47 |
+
st.write(f"• File Name: {result['metadata']['file_name']}")
|
| 48 |
+
st.write(f"• Page: {result['metadata']['page_label']}")
|
| 49 |
+
st.write(f"• Type: {result['metadata']['file_type']}")
|
| 50 |
+
st.write(f"• Size: {format_file_size(result['metadata']['file_size'])}")
|
| 51 |
+
|
| 52 |
+
with col2:
|
| 53 |
+
st.write(f"• Created: {result['metadata']['creation_date']}")
|
| 54 |
+
st.write(f"• Modified: {result['metadata']['last_modified_date']}")
|
| 55 |
+
|
| 56 |
+
st.markdown("#### 📝 Content")
|
| 57 |
+
st.markdown(f"<div style='background-color:#d3f9d8; padding: 10px; color: black;'><pre>{result['text']}</pre></div>", unsafe_allow_html=True)
|
| 58 |
+
|
| 59 |
+
access_token = os.getenv('eRAG_access_token')
|
| 60 |
+
headers = {
|
| 61 |
+
"Authorization": f"Bearer {access_token}"
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
st.markdown("""
|
| 65 |
+
<style>
|
| 66 |
+
.stTextInput>div>div>input {
|
| 67 |
+
background-color: #d3f9d8;
|
| 68 |
+
color: black;
|
| 69 |
+
cursor: text;
|
| 70 |
+
caret-color: black;
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
.stTextInput>div>div>input:focus {
|
| 74 |
+
border-color: black;
|
| 75 |
+
outline-color: black;
|
| 76 |
+
}
|
| 77 |
+
</style>
|
| 78 |
+
""", unsafe_allow_html=True)
|
| 79 |
+
|
| 80 |
+
api_url = os.getenv('api_url')
|
| 81 |
+
with st.form(key="input_form"):
|
| 82 |
+
user_input = st.text_input("ป้อนคำ ข้อความ หรือประโยคที่ต้องการค้นหา:", value=" องค์กรปกครองส่วนท้องถิ่น คืออะไร", key="input")
|
| 83 |
+
submit_button = st.form_submit_button("Send")
|
| 84 |
+
|
| 85 |
+
if submit_button and user_input:
|
| 86 |
+
st.write(f"คุณป้อนข้อความ: {user_input}")
|
| 87 |
+
try:
|
| 88 |
+
response = requests.post(api_url, json={"query": user_input}, headers=headers)
|
| 89 |
+
response.raise_for_status()
|
| 90 |
+
data = response.json()
|
| 91 |
+
search_results = data["results"]
|
| 92 |
+
|
| 93 |
+
st.markdown("### 🔎 Search Results")
|
| 94 |
+
for idx, result in enumerate(search_results):
|
| 95 |
+
display_search_result(result, idx)
|
| 96 |
+
|
| 97 |
+
except requests.RequestException as e:
|
| 98 |
+
st.error(f"Error: {str(e)}")
|
| 99 |
+
|
| 100 |
+
st.subheader("📄 ไฟล์เอกสารที่ใช้เป็นคลังข้อมูล PDPA")
|
| 101 |
+
st.markdown(
|
| 102 |
+
"""
|
| 103 |
+
- [พระราชบัญญัติการเลือกตั้งสมาชิกสภาท้องถิ่นหรือผู้บริหารท้องถิ่น พ.ศ. 2562.pdf](https://huggingface.co/spaces/amornpan/eRAG-Election/blob/main/พระราชบัญญัติการเลือกตั้งสมาชิกส���าท้องถิ่นหรือผู้บริหารท้องถิ่น พ.ศ. 2562.pdf) (103 kB)
|
| 104 |
+
""",
|
| 105 |
+
unsafe_allow_html=True
|
| 106 |
+
)
|
| 107 |
+
|