Triyono2026 commited on
Commit
54b4895
Β·
verified Β·
1 Parent(s): f739f42

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +44 -39
src/streamlit_app.py CHANGED
@@ -1,12 +1,3 @@
1
- # -*- coding: utf-8 -*-
2
- """Untitled0.ipynb
3
-
4
- Automatically generated by Colab.
5
-
6
- Original file is located at
7
- https://colab.research.google.com/drive/1XzCraWM_cfHaZvcAoEw56ClMXCmJG3TN
8
- """
9
-
10
  import streamlit as st
11
  import requests
12
  import os
@@ -14,79 +5,93 @@ import os
14
  # Konfigurasi Halaman
15
  st.set_page_config(page_title="AI Video Scene Architect", page_icon="🎬", layout="wide")
16
 
17
- # Model yang lebih stabil (V0.3 lebih baru dan jarang error 410)
18
- MODEL_URL = "https://router.huggingface.co/models/Mistralai/Mistral-7B-Instruct-v0.2"
 
 
19
 
20
  def query_ai(prompt, token):
21
  headers = {"Authorization": f"Bearer {token}"}
22
  payload = {
23
  "inputs": f"<s>[INST] {prompt} [/INST]",
24
- "parameters": {"max_new_tokens": 2000, "temperature": 0.7}
 
 
 
 
25
  }
26
- response = requests.post(MODEL_URL, headers=headers, json=payload)
27
  return response
28
 
29
  # --- UI INTERFACE ---
30
  st.title("🎬 AI Video Scene Architect")
31
 
32
- # Ambil token dari Secrets
33
  hf_token = os.environ.get("HF_TOKEN")
34
 
35
  with st.sidebar:
36
  st.header("βš™οΈ Pengaturan")
37
  if not hf_token:
38
  hf_token = st.text_input("Masukkan Hugging Face Token", type="password")
 
39
 
40
  num_scenes = st.slider("Jumlah Scene", 1, 20, 5)
41
 
42
- # Fitur Baru: Aspect Ratio
43
  aspect_ratio = st.selectbox(
44
- "Aspect Ratio (Ukuran Video)",
45
- ["16:9 (Landscape/YouTube)", "9:16 (Portrait/TikTok)", "2:3 (Vertical)", "4:5 (Instagram)"]
46
  )
47
 
48
- style = st.selectbox("Gaya Visual", ["Cinematic", "Realistic", "Anime", "3D Animation"])
49
 
50
  # Input User
51
  col1, col2 = st.columns(2)
52
  with col1:
53
- idea = st.text_area("Ide Utama Video", placeholder="Contoh: Pemuda di rumah makan nasi kandar...", height=120)
54
  with col2:
55
- details = st.text_area("Detail Visual", placeholder="Siang hari, terang, cuaca cerah...", height=120)
56
 
57
  if st.button("πŸš€ Generate Sequence Prompts"):
58
  if not hf_token:
59
- st.error("❌ Token belum diisi! Masukkan di sidebar atau Settings > Secrets.")
60
  elif not idea:
61
- st.warning("⚠️ Masukkan ide video dulu.")
62
  else:
63
- with st.spinner("Sedang merancang rangkaian scene..."):
64
- # Prompt Engineering yang lebih ketat
65
- prompt_instruction = f"""
66
- Task: Create {num_scenes} sequential video prompts.
67
- Topic: {idea}
68
- Details: {details}
69
  Visual Style: {style}
70
- Target Aspect Ratio: {aspect_ratio}
71
- Duration per scene: 5 seconds.
 
 
 
 
72
 
73
- Format output as:
74
- Scene [Number] ([Duration]): [Detailed visual prompt including style and aspect ratio]
75
  """
76
 
77
- res = query_ai(prompt_instruction, hf_token)
78
 
79
  if res.status_code == 200:
80
  result = res.json()
 
81
  output_text = result[0]['generated_text'] if isinstance(result, list) else result.get('generated_text', '')
82
 
83
- # Menampilkan hasil
84
  st.success("βœ… Rangkaian Scene Berhasil Dibuat!")
85
  st.markdown("---")
86
 
87
- # Membersihkan teks dari instruksi awal jika terbawa
88
- clean_text = output_text.split("[/INST]")[-1]
89
- st.write(clean_text)
 
 
 
 
90
  else:
91
- st.error(f"❌ Error {res.status_code}: {res.text}")
92
- st.info("Tips: Jika error 401, token salah. Jika 503, tunggu 1 menit lalu klik lagi.")
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import requests
3
  import os
 
5
  # Konfigurasi Halaman
6
  st.set_page_config(page_title="AI Video Scene Architect", page_icon="🎬", layout="wide")
7
 
8
+ # Menggunakan Model yang mendukung API Router terbaru
9
+ # Kita gunakan Mistral 7B v0.3 yang sangat stabil
10
+ MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"
11
+ API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
12
 
13
  def query_ai(prompt, token):
14
  headers = {"Authorization": f"Bearer {token}"}
15
  payload = {
16
  "inputs": f"<s>[INST] {prompt} [/INST]",
17
+ "parameters": {
18
+ "max_new_tokens": 1500,
19
+ "temperature": 0.7,
20
+ "wait_for_model": True # Penting: Menunggu jika model sedang loading
21
+ }
22
  }
23
+ response = requests.post(API_URL, headers=headers, json=payload)
24
  return response
25
 
26
  # --- UI INTERFACE ---
27
  st.title("🎬 AI Video Scene Architect")
28
 
29
+ # Ambil token dari Secrets (HF_TOKEN)
30
  hf_token = os.environ.get("HF_TOKEN")
31
 
32
  with st.sidebar:
33
  st.header("βš™οΈ Pengaturan")
34
  if not hf_token:
35
  hf_token = st.text_input("Masukkan Hugging Face Token", type="password")
36
+ st.caption("Dapatkan di: huggingface.co/settings/tokens")
37
 
38
  num_scenes = st.slider("Jumlah Scene", 1, 20, 5)
39
 
 
40
  aspect_ratio = st.selectbox(
41
+ "Ukuran Video (Aspect Ratio)",
42
+ ["16:9 (YouTube)", "9:16 (TikTok/Reels)", "2:3 (Vertical)", "4:5 (Instagram)"]
43
  )
44
 
45
+ style = st.selectbox("Gaya Visual", ["Cinematic", "Realistic Photography", "Anime Style", "3D Animation"])
46
 
47
  # Input User
48
  col1, col2 = st.columns(2)
49
  with col1:
50
+ idea = st.text_area("Ide Utama Video", placeholder="Contoh: Petualangan robot di hutan hujan...", height=120)
51
  with col2:
52
+ details = st.text_area("Detail Visual", placeholder="Pencahayaan dramatis, warna cerah, detail tinggi...", height=120)
53
 
54
  if st.button("πŸš€ Generate Sequence Prompts"):
55
  if not hf_token:
56
+ st.error("❌ Token tidak ditemukan! Masukkan di sidebar atau di Settings Space.")
57
  elif not idea:
58
+ st.warning("⚠️ Silakan tulis ide video Anda.")
59
  else:
60
+ with st.spinner("Menghubungi AI... Mohon tunggu sebentar."):
61
+ # Instruksi yang memaksa AI memberikan hasil akurat per 5 detik
62
+ system_prompt = f"""
63
+ Create a storyboard of {num_scenes} sequential scenes.
64
+ Video Topic: {idea}
65
+ Specific Details: {details}
66
  Visual Style: {style}
67
+ Aspect Ratio: {aspect_ratio}
68
+
69
+ Strict Rule:
70
+ - Each scene MUST be exactly 5 seconds long.
71
+ - Ensure visual continuity between scenes.
72
+ - Provide a detailed prompt for each scene suitable for AI Video Generators.
73
 
74
+ Format:
75
+ Scene [Number] (5s): [Visual Prompt]
76
  """
77
 
78
+ res = query_ai(system_prompt, hf_token)
79
 
80
  if res.status_code == 200:
81
  result = res.json()
82
+ # Penanganan jika output berbentuk list atau dict
83
  output_text = result[0]['generated_text'] if isinstance(result, list) else result.get('generated_text', '')
84
 
 
85
  st.success("βœ… Rangkaian Scene Berhasil Dibuat!")
86
  st.markdown("---")
87
 
88
+ # Pembersihan teks dari instruksi awal
89
+ final_output = output_text.split("[/INST]")[-1].strip()
90
+ st.write(final_output)
91
+ elif res.status_code == 503:
92
+ st.warning("πŸ”„ Model sedang disiapkan oleh Hugging Face. Silakan tunggu 30 detik lalu klik 'Generate' lagi.")
93
+ elif res.status_code == 404:
94
+ st.error("❌ Error 404: Model tidak ditemukan atau URL salah. Pastikan nama model benar.")
95
  else:
96
+ st.error(f"❌ Terjadi kesalahan (Error {res.status_code}).")
97
+ st.info(f"Pesan: {res.text}")