[INST]
You are an expert in South American indigenous languages.
Use strictly and only the information below to answer the user question in **English**.
- Do not infer or assume facts that are not explicitly stated.
- If the answer is unknown or insufficient, say \"I cannot answer with the available data.\"
- Limit your answer to 100 words.
### CONTEXT:
{chr(10).join(context)}
### RDF RELATIONS:
{chr(10).join(rdf_facts)}
### QUESTION:
{user_question}
Answer:
[/INST]"""
try:
res = requests.post(
ENDPOINT_URL,
headers={"Authorization": f"Bearer {HF_API_TOKEN}", "Content-Type": "application/json"},
json={"inputs": prompt}, timeout=60
)
out = res.json()
if isinstance(out, list) and "generated_text" in out[0]:
return out[0]["generated_text"].replace(prompt.strip(), "").strip(), ids, context, rdf_facts
return str(out), ids, context, rdf_facts
except Exception as e:
return str(e), ids, context, rdf_facts
# === MAIN FUNCTION ===
def main():
st.markdown("""
Why this matters: Many indigenous languages in South America are disappearing. This app helps understand and preserve them using artificial intelligence.
""", unsafe_allow_html=True)
with st.sidebar:
st.image("https://glottolog.org/static/img/glottolog_lod.png", width=180)
st.markdown("### What are the methods?")
st.markdown("""
- **Graph A**: Combines descriptions, country info, and speaker data using classic node2vec embeddings.
- **Graph B**: Uses graph learning (GraphSAGE) to detect patterns in how languages relate to each other.
""")
st.markdown("### Options")
k = st.slider("How many languages to analyze?", 1, 10, 3)
show_ids = st.checkbox("Show IDs", value=True)
show_ctx = st.checkbox("Show Text Info", value=True)
show_rdf = st.checkbox("Show Extra Facts", value=True)
query = st.text_input("Ask something about South American languages:", "What languages are spoken in Perú?")
if st.button("Analyze") and query:
col1, col2 = st.columns(2)
results = {}
for col, (label, method) in zip([col1, col2], methods.items()):
with col:
st.subheader(f"{label} Method")
start = datetime.datetime.now()
response, lang_ids, context, rdf_data = generate_response(*method, query, k)
duration = (datetime.datetime.now() - start).total_seconds()
st.markdown(response)
st.markdown(f"⏱️ {duration:.2f}s | 🌐 {len(lang_ids)} languages")
if show_ids:
st.markdown("**Language IDs:**")
st.code("\n".join(lang_ids))
if show_ctx:
st.markdown("**Text Info:**")
st.markdown("\n\n---\n\n".join(context))
if show_rdf:
st.markdown("**Extra Facts:**")
st.code("\n".join(rdf_data))
if __name__ == "__main__":
main()