ngoccs commited on
Commit
3ca4ccb
·
verified ·
1 Parent(s): 8915fd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -21
app.py CHANGED
@@ -1,21 +1,33 @@
1
- import streamlit as st
2
- import google.generativeai as genai
3
- import os
4
-
5
- # Cấu hình giao diện
6
- st.set_page_config(page_title="My AI App", page_icon="🤖")
7
- st.title("Ứng dụng AI của tôi")
8
-
9
- # Lấy API Key từ biến môi trường (an toàn hơn)
10
- api_key = os.getenv("GEMINI_API_KEY")
11
-
12
- if api_key:
13
- genai.configure(api_key=api_key)
14
- model = genai.GenerativeModel('gemini-2.5-flash')
15
-
16
- user_input = st.text_input("Nhập câu hỏi của bạn:")
17
- if st.button("Gửi"):
18
- response = model.generate_content(user_input)
19
- st.write(response.text)
20
- else:
21
- st.error("Thiếu API Key! Hãy cấu hình trong phần Settings.")
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import google.generativeai as genai
3
+ import os
4
+
5
+ # 1. Cấu hình giao diện trang web
6
+ st.set_page_config(page_title="My AI App", page_icon="🤖")
7
+ st.title("Ứng dụng AI của tôi")
8
+
9
+ # 2. Lấy API Key từ mục Settings > Secrets của Hugging Face
10
+ api_key = os.getenv("GEMINI_API_KEY")
11
+
12
+ if api_key:
13
+ # Cấu hình mô hình Gemini
14
+ genai.configure(api_key=api_key)
15
+ model = genai.GenerativeModel('gemini-1.5-flash') # Hoặc gemini-2.0-flash tùy phiên bản bạn dùng
16
+
17
+ # 3. Giao diện nhập liệu
18
+ user_input = st.text_input("Nhập câu hỏi của bạn:", placeholder="Ví dụ: Cách nấu phở bò...")
19
+
20
+ if st.button("Gửi"):
21
+ if user_input:
22
+ with st.spinner('Đang suy nghĩ...'):
23
+ try:
24
+ response = model.generate_content(user_input)
25
+ st.markdown("### Trả lời:")
26
+ st.write(response.text)
27
+ except Exception as e:
28
+ st.error(f"Lỗi rồi: {e}")
29
+ else:
30
+ st.warning("Vui lòng nhập nội dung câu hỏi!")
31
+ else:
32
+ # Thông báo nếu bạn chưa cài đặt API Key trong phần Secret
33
+ st.error("Thiếu API Key! Hãy vào tab Settings > Variables and secrets để thêm GEMINI_API_KEY.")