milanmor commited on
Commit
a4b5810
·
verified ·
1 Parent(s): b3300a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -33
app.py CHANGED
@@ -1,71 +1,76 @@
1
  import streamlit as st
2
 
3
- # ⚙️ Oldal beállítása – csak egyszer és elsőként hívható
4
- st.set_page_config(page_title="Major Plato Military Ethics Decision Maker Simulator", layout="wide")
5
 
6
  import os
7
  from huggingface_hub import InferenceClient
8
 
9
- # 🧭 API provider választás
10
  st.sidebar.header("API beállítások")
11
- provider_choice = st.sidebar.selectbox(
12
- "Inference provider",
13
- options=["hf-inference (free)", "custom key"],
14
- help="Válassz: ingyenes HF inferencia vagy saját fizetős API-kulcs"
15
  )
16
 
17
  custom_key = ""
18
- if provider_choice == "custom key":
19
- custom_key = st.sidebar.text_input("Írd be a szolgáltatód API-kulcsát", type="password")
20
 
21
- # HF token titkos változóból
 
 
 
 
 
 
 
 
22
  HF_TOKEN = st.secrets["HF_TOKEN"]
23
- if provider_choice == "hf-inference (free)":
24
- client = InferenceClient(token=HF_TOKEN, provider="hf-inference")
25
- else:
26
  if not custom_key:
27
- st.sidebar.error("Hiányzik a saját szolgáltató API-kulcs.")
28
  st.stop()
29
  client = InferenceClient(token=HF_TOKEN, provider="custom", api_key=custom_key)
 
 
30
 
31
- # Oldal címe
32
  st.title("🎖️ Major Plato – Katonai Etikai Szimuláció")
33
-
34
- # Rendszer prompt betöltése
35
  if os.path.exists("system.txt"):
36
  system = open("system.txt", encoding="utf-8").read().strip()
37
  else:
38
- st.error("Hiba: Nincs 'system.txt' fájl! Ide írd Major Plato karakter leírását.")
39
  st.stop()
40
 
41
- # Forgatókönyv feltöltése vagy kézi kérdés
42
  scenario = ""
43
- uploaded = st.file_uploader("Töltsd fel forgatókönyv fájlt (.txt):", type="txt")
44
  if uploaded:
45
  scenario = uploaded.read().decode("utf-8")
 
46
 
47
- user_in = st.text_area("Vagy írd be kézzel a kérdésedet:")
48
-
49
- # Paraméterek az oldalsávban
50
- max_tokens = st.sidebar.slider("Max token", 100, 1000, 600, 50)
51
  temperature = st.sidebar.slider("Temperature", 0.2, 1.0, 0.7, 0.1)
52
 
53
- # CSS a scrollozható expanderhez
54
  st.markdown(
55
  """
56
  <style>
57
  [data-testid="stExpander"] div[role="button"] + div {
58
- max-height: 400px;
59
  overflow-y: auto;
60
  }
61
  </style>
62
- """,
63
- unsafe_allow_html=True
64
  )
65
 
66
- if st.button("Command!"):
 
67
  if not (scenario.strip() or user_in.strip()):
68
- st.error("Kérlek, tölts fel forgatókönyvet vagy írj kérdést!")
69
  else:
70
  usr_content = scenario.strip()
71
  if user_in.strip():
@@ -78,16 +83,16 @@ if st.button("Command!"):
78
 
79
  with st.spinner("Major Plato gondolkodik..."):
80
  stream = client.chat_completion(
81
- model="meta-llama/Llama-3.1-8B-Instruct",
82
  messages=messages,
83
  max_tokens=max_tokens,
84
  temperature=temperature,
85
- stream=True # folyamatos válasz fogadó
86
  )
87
 
88
  placeholder = st.empty()
89
  full_resp = ""
90
- with st.expander("🗣️ Major Plato answering:", expanded=True):
91
  for chunk in stream:
92
  delta = chunk.choices[0].delta.content
93
  if delta:
 
1
  import streamlit as st
2
 
3
+ # ⚙️ Oldal beállítása – egyszer, script elején
4
+ st.set_page_config(page_title="Major Plato Szimulátor", layout="wide")
5
 
6
  import os
7
  from huggingface_hub import InferenceClient
8
 
9
+ # 🧭 API provider választás a bal oldalon
10
  st.sidebar.header("API beállítások")
11
+ provider = st.sidebar.selectbox(
12
+ "Provider",
13
+ options=["auto (HF vagy más)", "custom"],
14
+ help="‘auto’: legjobb elérhető provider; 'custom': írd be saját API-kulcsod"
15
  )
16
 
17
  custom_key = ""
18
+ if provider == "custom":
19
+ custom_key = st.sidebar.text_input("Saját API-kulcs", type="password")
20
 
21
+ # Modellválasztás
22
+ models = [
23
+ "meta-llama/Llama-3.1-8B-Instruct",
24
+ "deepseek-ai/DeepSeek-R1",
25
+ "gpt2"
26
+ ]
27
+ model_choice = st.sidebar.selectbox("Válaszd ki a modellt:", models)
28
+
29
+ # InferenceClient konfigurálása
30
  HF_TOKEN = st.secrets["HF_TOKEN"]
31
+ if provider == "custom":
 
 
32
  if not custom_key:
33
+ st.sidebar.error("Hiányzik az API-kulcs!")
34
  st.stop()
35
  client = InferenceClient(token=HF_TOKEN, provider="custom", api_key=custom_key)
36
+ else:
37
+ client = InferenceClient(token=HF_TOKEN, provider="auto")
38
 
39
+ # Cím és rendszer prompt betöltése
40
  st.title("🎖️ Major Plato – Katonai Etikai Szimuláció")
 
 
41
  if os.path.exists("system.txt"):
42
  system = open("system.txt", encoding="utf-8").read().strip()
43
  else:
44
+ st.error("Hiba: nincs system.txt! Add meg Major Plato karakterét.")
45
  st.stop()
46
 
47
+ # Forgatókönyv feltöltés vagy kézi kérdés
48
  scenario = ""
49
+ uploaded = st.file_uploader("Forgatókönyv fájl feltöltése (.txt)", type="txt")
50
  if uploaded:
51
  scenario = uploaded.read().decode("utf-8")
52
+ user_in = st.text_area("Kézi kérdés Major Plato számára:")
53
 
54
+ # Paraméterek a bal sávban
55
+ max_tokens = st.sidebar.slider("Max token", 200, 1500, 800, 50)
 
 
56
  temperature = st.sidebar.slider("Temperature", 0.2, 1.0, 0.7, 0.1)
57
 
58
+ # CSS a scrollbar expanderhez
59
  st.markdown(
60
  """
61
  <style>
62
  [data-testid="stExpander"] div[role="button"] + div {
63
+ max-height: 500px;
64
  overflow-y: auto;
65
  }
66
  </style>
67
+ """, unsafe_allow_html=True
 
68
  )
69
 
70
+ # Kérdés gomb
71
+ if st.button("Indítás"):
72
  if not (scenario.strip() or user_in.strip()):
73
+ st.error("Adj forgatókönyvet vagy írj kérdést!")
74
  else:
75
  usr_content = scenario.strip()
76
  if user_in.strip():
 
83
 
84
  with st.spinner("Major Plato gondolkodik..."):
85
  stream = client.chat_completion(
86
+ model=model_choice,
87
  messages=messages,
88
  max_tokens=max_tokens,
89
  temperature=temperature,
90
+ stream=True
91
  )
92
 
93
  placeholder = st.empty()
94
  full_resp = ""
95
+ with st.expander("🗣️ Major Plato válasza:", expanded=True):
96
  for chunk in stream:
97
  delta = chunk.choices[0].delta.content
98
  if delta: