nzde commited on
Commit
43cc02f
·
verified ·
1 Parent(s): 75bda03

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +34 -0
  2. requirements.txt +3 -3
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import pipeline
4
+
5
+ # 1. 網頁標題與外觀設定
6
+ st.set_page_config(page_title="AI Python 代碼助手", page_icon="🤖")
7
+ st.title("🤖 專屬 AI 程式碼自動補全助手")
8
+ st.markdown("輸入你的 Python 註解或變數,AI 將自動幫你寫完後續的程式碼!")
9
+
10
+ # 2. 載入模型的快取機制 (避免每次輸入都重新載入 500MB 的模型)
11
+ @st.cache_resource
12
+ def load_model():
13
+ device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
14
+ pipe = pipeline("text-generation", model="huggingface-course/codeparrot-ds", device=device)
15
+ return pipe
16
+
17
+ pipe = load_model()
18
+
19
+ # 3. 建立使用者輸入區
20
+ user_input = st.text_area("請輸入程式碼註解 (例如:# create a scatter plot):", height=150)
21
+
22
+ # 4. 建立生成按鈕與輸出邏輯
23
+ if st.button("✨ 讓 AI 幫我寫程式"):
24
+ if user_input:
25
+ with st.spinner("AI 正在思考中..."):
26
+ # 執行推論
27
+ result = pipe(user_input, max_new_tokens=50, num_return_sequences=1)[0]["generated_text"]
28
+
29
+ st.success("生成成功!")
30
+ st.subheader("💡 生成結果:")
31
+ # 用漂亮的程式碼區塊顯示結果
32
+ st.code(result, language="python")
33
+ else:
34
+ st.warning("請先輸入一些註解或代碼喔!")
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
- altair
2
- pandas
3
- streamlit
 
1
+ streamlit
2
+ torch
3
+ transformers