Spaces:
Running
Running
File size: 9,173 Bytes
efa947c da792f9 efa947c 9f2350c efa947c c198e2b efa947c c198e2b efa947c c198e2b efa947c c198e2b efa947c c198e2b e64b7eb c198e2b efa947c c198e2b efa947c c198e2b efa947c c198e2b efa947c c198e2b efa947c 5892a3b 57e9be6 5892a3b efa947c 5892a3b efa947c 5892a3b efa947c 5892a3b efa947c e64b7eb 7dc7a24 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
#--------------------IMPORTED LIBRARIES-----------------------------
import os
os.environ['TRANSFORMERS_CACHE'] = '/tmp/hf'
os.environ['HF_HOME'] = '/tmp/hf'
import streamlit as st
import base64
import json
import faiss
import torch
from transformers import AutoTokenizer, AutoModel
import torch.nn.functional as F
import httpx
from huggingface_hub import hf_hub_download
# ---------------------- INITIAL CONFIGURATION ----------------------
st.set_page_config(page_title="PoliticBot", layout="wide")
with open("fondo.jpeg", "rb") as f:
img_bytes = f.read()
encoded_img = base64.b64encode(img_bytes).decode()
st.markdown(f"""
<style>
.stApp {{
background-image: url("data:image/jpeg;base64,{encoded_img}");
background-size: cover;
background-repeat: no-repeat;
background-attachment: scroll;
}}
section[data-testid="stSidebar"] {{
background-color: rgba(0, 0, 50, 0.6);
color: white;
}}
h1, h2, h3, h4, h5, h6, p, label, div, span {{
color: white !important;
}}
textarea {{
color: white !important;
background-color: rgba(0, 0, 0, 0.3) !important;
border: 1px solid #ccc !important;
border-radius: 8px !important;
padding: 0.5em !important;
min-height: 100px !important;
transition: none !important;
}}
::placeholder {{
color: #ccc !important;
}}
pre, code {{
background-color: rgba(0, 0, 0, 0.4) !important;
color: white !important;
border-radius: 8px !important;
padding: 0.5em !important;
}}
div[data-testid="stExpander"] {{
transition: none !important;
}}
* {{
transition: none !important;
animation: none !important;
}}
section[data-testid="stSidebar"] div[data-testid="stButton"] > button {{
background-color: #526366 !important;
color: white !important;
font-weight: bold;
font-size: 16px;
border-radius: 8px;
padding: 0.6em;
width: 80% !important;
margin-bottom: 0.5em;
}}
div[data-testid="stButton"] > button {{
background-color: #526366 !important;
color: white !important;
font-weight: bold;
font-size: 16px;
border-radius: 8px;
padding: 0.6em;
margin-top: 1em;
}}
</style>
""", unsafe_allow_html=True)
# ---------------------- LIBRARIES AND MODELS ----------------------
ideology_families = ["Communism", "Liberalism", "Conservatism", "Fascism", "Radical_Left"]
ideology_keywords = {
"Communism": ["communism", "marxism", "marxist", "anarcho-communism", "leninism"],
"Liberalism": ["liberalism", "libertarianism", "classical liberal"],
"Conservatism": ["conservatism", "traditional conservatism", "neoconservatism"],
"Fascism": ["fascism", "nazism", "national socialism"],
"Radical_Left": ["radical left", "far-left", "revolutionary socialism", "anarchism"]
}
@st.cache_resource
def load_encoder():
model_name = "intfloat/e5-base-v2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name).to("cuda" if torch.cuda.is_available() else "cpu")
return tokenizer, model
tokenizer, model = load_encoder()
def mean_pooling(output, mask):
token_embeddings = output.last_hidden_state
input_mask_expanded = mask.unsqueeze(-1).expand(token_embeddings.size())
return (token_embeddings * input_mask_expanded).sum(1) / input_mask_expanded.sum(1)
def embed_query(query):
prefixed = f"query: {query}"
inputs = tokenizer(prefixed, return_tensors='pt', truncation=True, padding=True, max_length=512)
inputs = {k: v.to(model.device) for k, v in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
pooled = mean_pooling(outputs, inputs["attention_mask"])
return F.normalize(pooled, p=2, dim=1).cpu().numpy().astype("float32")
@st.cache_resource
def load_data_global():
chunks_path = hf_hub_download(repo_id="Bartix84/politicbot-data", filename="chunks.jsonl", repo_type="dataset")
index_path = hf_hub_download(repo_id="Bartix84/politicbot-data", filename="faiss_index.index", repo_type="dataset")
metadata_path = hf_hub_download(repo_id="Bartix84/politicbot-data", filename="metadata_titles.json", repo_type="dataset")
index = faiss.read_index(index_path)
with open(metadata_path, "r", encoding="utf-8") as f:
metadata = json.load(f)
with open(chunks_path, "r", encoding="utf-8") as f:
chunks = [json.loads(line) for line in f]
return index, metadata, chunks
def search_in_global_index(query_embedding, index, metadata, chunks, selected_ideology, k=5):
_, indices = index.search(query_embedding, k * 8)
results = []
keywords = ideology_keywords.get(selected_ideology, [])
seen_titles = set()
for i in range(indices.shape[1]):
idx = indices[0][i]
title = metadata[idx]
if title in seen_titles:
continue
seen_titles.add(title)
match = next((chunk for chunk in chunks if chunk["title"] == title), None)
if match:
title_text = title.lower()
if any(keyword in title_text for keyword in keywords):
results.append(match)
if len(results) >= k:
break
return results
def generate_rag_response(ideology, user_query, context_chunks):
context = "\n\n".join(chunk["chunk"] for chunk in context_chunks)[:1500]
system_prompt = f"You are a political assistant who thinks and reasons like a {ideology} thinker."
user_prompt = f"""
Answer the following political or ethical question based strictly on the CONTEXT provided.
Think according to the principles and values of {ideology}. If the context is insufficient, clearly say so or explain its limitations.
Avoid always starting your answer the same way. Vary the introduction while staying formal and ideologically grounded.
CONTEXT:
{context}
QUESTION:
{user_query}
ANSWER:"""
# Verificar si existe la API Key como secreto
api_key = os.getenv("OPENROUTER_API_KEY")
if not api_key:
return "❌ Error: Missing `OPENROUTER_API_KEY` in Hugging Face Space secrets."
headers = {
"Authorization": f"Bearer {api_key}",
"HTTP-Referer": "https://yourappname.streamlit.app",
"X-Title": "PoliticBot"
}
payload = {
"model": "mistralai/mistral-7b-instruct",
"messages": [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt}
],
"temperature": 0.9,
"max_tokens": 768,
"top_p": 0.95
}
try:
response = httpx.post(
"https://openrouter.ai/api/v1/chat/completions",
headers=headers,
json=payload,
timeout=60
)
response.raise_for_status()
except httpx.RequestError as e:
return f"❌ Connection error: {e}"
except httpx.HTTPStatusError as e:
return f"❌ API error: {e.response.status_code} - {e.response.text}"
return response.json()["choices"][0]["message"]["content"].strip()
# ---------------------- STREAMLIT INTERFACE ----------------------
st.image('portada3.jpg', use_container_width=True)
st.title('🗳️ PoliticBot')
st.subheader('Reasoning with political ideologies')
with st.sidebar:
st.header("Choose a political ideology")
if "selected_ideology" not in st.session_state:
st.session_state.selected_ideology = None
for ideology in ideology_families:
if st.button(ideology):
st.session_state.selected_ideology = ideology
selected_ideology = st.session_state.selected_ideology
if selected_ideology:
st.write(f"You have selected: **{selected_ideology}**")
user_query = st.text_area("Write your question or political dilemma:", height=100, key="user_input")
if selected_ideology and st.button("Send question"):
if user_query.strip() == "":
st.warning("Write a question before continuing.")
else:
with st.spinner("Thinking like that ideology..."):
query_emb = embed_query(user_query + " in the context of " + selected_ideology)
index, metadata, chunks = load_data_global()
context = search_in_global_index(query_emb, index, metadata, chunks, selected_ideology, k=5)
response = generate_rag_response(selected_ideology, user_query, context)
st.session_state.response = response
st.session_state.context = context
st.session_state.last_query = user_query
if "response" in st.session_state and st.session_state.selected_ideology:
st.subheader("🤖 Generated response:")
st.markdown(f"> {st.session_state.response}")
with st.expander("🌐 Display the context used"):
context = st.session_state.context
if not context:
st.markdown("*No relevant context found.*")
else:
for chunk in context:
st.markdown(f"**{chunk['title']}**")
st.code(chunk["chunk"][:500] + "...") |