File size: 1,207 Bytes
2fe3b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edaac00
2fe3b7f
 
 
 
 
 
 
 
 
 
 
edaac00
2fe3b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import streamlit as st
from smolagents import LiteLLMModel
import os

def load_model():
    MODEL_NAMES = [
        "Qwen/QwQ-32B",  # 免費使用
    ]
    model = LiteLLMModel(
        "together_ai" + "/" + MODEL_NAMES[0],
        temperature=0.2,
        api_key=os.getenv("TOGETHER_API_KEY")
    )
    return model

def main():
    st.title("智慧文件摘要")
    st.markdown("上傳文本檔案,生成繁中重點摘要")

    # 讀取 API_KEY 從 Environment Variables
    api_key = os.getenv("TOGETHER_API_KEY")
    if not api_key:
        st.error("請設定 Together AI API_KEY!")
        return

    # 讓用戶上傳文件
    uploaded_file = st.file_uploader("上傳 txt 文件", type=["txt"])
    if not uploaded_file:
        st.info("請選擇文件...")
        return

    # 讀取文件內容
    content = uploaded_file.read().decode("utf-8")

    # 生成摘要
    model = load_model()
    response = model([{"role": "user", "content": "針對本文幫我生成繁中重點摘要並包含Markdown:" + content}])
    summary = str(response.content)

    # 顯示結果
    st.markdown("### 生成的摘要如下:")
    st.markdown(summary)

if __name__ == "__main__":
    main()