Spaces:
Sleeping
Sleeping
import streamlit as st | |
# ⚙️ Oldal beállítása – egyszer, script elején | |
st.set_page_config(page_title="Major Plato Szimulátor", layout="wide") | |
import os | |
from huggingface_hub import InferenceClient | |
# 🧭 API provider választás a bal oldalon | |
st.sidebar.header("API beállítások") | |
provider = st.sidebar.selectbox( | |
"Provider", | |
options=["auto (HF vagy más)", "custom"], | |
help="‘auto’: legjobb elérhető provider; 'custom': írd be saját API-kulcsod" | |
) | |
custom_key = "" | |
if provider == "custom": | |
custom_key = st.sidebar.text_input("Saját API-kulcs", type="password") | |
# Modellválasztás | |
models = [ | |
"meta-llama/Llama-3.1-8B-Instruct", | |
"deepseek-ai/DeepSeek-R1", | |
"google/gemma-3-27b-it", | |
"NousResearch/Llama-2-70b-hf", | |
"meta-llama/Llama-2-70b-chat-hf", | |
"google/gemma-2-27b-it", | |
"google/gemma-2-9b-it", | |
"google/gemma-2-2b-it", | |
"xai-org/grok-1", | |
"amazon/MistralLite", | |
"mistralai/Mixtral-8x7B-Instruct-v0.1", | |
"gpt2", | |
"google/gemma-2-2b-it", | |
"meta-llama/Llama-2-70b-hf" | |
] | |
model_choice = st.sidebar.selectbox("Válaszd ki a modellt:", models) | |
# InferenceClient konfigurálása | |
HF_TOKEN = st.secrets["HF_TOKEN"] | |
if provider == "custom": | |
if not custom_key: | |
st.sidebar.error("Hiányzik az API-kulcs!") | |
st.stop() | |
client = InferenceClient(token=HF_TOKEN, provider="custom", api_key=custom_key) | |
else: | |
client = InferenceClient(token=HF_TOKEN, provider="auto") | |
# Cím és rendszer prompt betöltése | |
st.title("🎖️ Major Plato – Katonai Etikai Szimuláció") | |
if os.path.exists("system.txt"): | |
system = open("system.txt", encoding="utf-8").read().strip() | |
else: | |
st.error("Hiba: nincs system.txt! Add meg Major Plato karakterét.") | |
st.stop() | |
# Forgatókönyv feltöltés vagy kézi kérdés | |
scenario = "" | |
uploaded = st.file_uploader("Forgatókönyv fájl feltöltése (.txt)", type="txt") | |
if uploaded: | |
scenario = uploaded.read().decode("utf-8") | |
user_in = st.text_area("Kézi kérdés Major Plato számára:") | |
# Paraméterek a bal sávban | |
max_tokens = st.sidebar.slider("Max token", 200, 1500, 800, 50) | |
temperature = st.sidebar.slider("Temperature", 0.2, 1.0, 0.7, 0.1) | |
# CSS a scrollbar expanderhez | |
st.markdown( | |
""" | |
<style> | |
[data-testid="stExpander"] div[role="button"] + div { | |
max-height: 500px; | |
overflow-y: auto; | |
} | |
</style> | |
""", unsafe_allow_html=True | |
) | |
# Kérdés gomb | |
if st.button("Indítás"): | |
if not (scenario.strip() or user_in.strip()): | |
st.error("Adj forgatókönyvet vagy írj kérdést!") | |
else: | |
usr_content = scenario.strip() | |
if user_in.strip(): | |
usr_content += "\n\n" + user_in.strip() | |
messages = [ | |
{"role": "system", "content": system}, | |
{"role": "user", "content": usr_content} | |
] | |
with st.spinner("Major Plato gondolkodik..."): | |
stream = client.chat_completion( | |
model=model_choice, | |
messages=messages, | |
max_tokens=max_tokens, | |
temperature=temperature, | |
stream=True | |
) | |
placeholder = st.empty() | |
full_resp = "" | |
with st.expander("🗣️ Major Plato válasza:", expanded=True): | |
for chunk in stream: | |
delta = chunk.choices[0].delta.content | |
if delta: | |
full_resp += delta | |
placeholder.markdown(full_resp) | |