Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
# 東吳大學資料系2025年LINEBOT
|
| 2 |
|
| 3 |
from flask import Flask, request, abort, send_from_directory
|
| 4 |
-
import pyimgur
|
| 5 |
|
| 6 |
import markdown
|
| 7 |
from bs4 import BeautifulSoup
|
|
@@ -36,16 +35,6 @@ import google.generativeai as genai
|
|
| 36 |
# HF_TOKEN = os.environ.get('HF_TOKEN')
|
| 37 |
# headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 38 |
|
| 39 |
-
imgur_client_id = os.environ.get('IMGUR_CLIENT_ID')
|
| 40 |
-
imgur_client = pyimgur.Imgur(imgur_client_id)
|
| 41 |
-
|
| 42 |
-
## 下面一段是測試是否imgur會檔huggingface space,測試完可刪除
|
| 43 |
-
|
| 44 |
-
uploaded_image = imgur_client.upload_image("/code/mi.png")
|
| 45 |
-
app.logger.info(uploaded_image.link)
|
| 46 |
-
|
| 47 |
-
##
|
| 48 |
-
|
| 49 |
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
|
| 50 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 51 |
model = genai.GenerativeModel('gemini-2.0-flash')
|
|
@@ -61,6 +50,15 @@ def query(payload):
|
|
| 61 |
|
| 62 |
app = Flask(__name__)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 65 |
app.logger.setLevel(logging.INFO)
|
| 66 |
|
|
@@ -119,7 +117,8 @@ def handle_content_message(event):
|
|
| 119 |
dist_path = tempfile_path + '.' + ext
|
| 120 |
dist_name = os.path.basename(dist_path)
|
| 121 |
os.rename(tempfile_path, dist_path)
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
with ApiClient(configuration) as api_client:
|
| 125 |
line_bot_api = MessagingApi(api_client)
|
|
@@ -128,7 +127,7 @@ def handle_content_message(event):
|
|
| 128 |
reply_token=event.reply_token,
|
| 129 |
messages=[
|
| 130 |
TextMessage(text='Save content.'),
|
| 131 |
-
TextMessage(text=
|
| 132 |
# TextMessage(text=request.host_url + os.path.join('static', 'tmp', dist_name))
|
| 133 |
]
|
| 134 |
)
|
|
|
|
| 1 |
# 東吳大學資料系2025年LINEBOT
|
| 2 |
|
| 3 |
from flask import Flask, request, abort, send_from_directory
|
|
|
|
| 4 |
|
| 5 |
import markdown
|
| 6 |
from bs4 import BeautifulSoup
|
|
|
|
| 35 |
# HF_TOKEN = os.environ.get('HF_TOKEN')
|
| 36 |
# headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
GOOGLE_API_KEY = os.environ.get('GOOGLE_API_KEY')
|
| 39 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 40 |
model = genai.GenerativeModel('gemini-2.0-flash')
|
|
|
|
| 50 |
|
| 51 |
app = Flask(__name__)
|
| 52 |
|
| 53 |
+
### 將/tmp資料夾中的圖片建立成URL
|
| 54 |
+
|
| 55 |
+
@app.route("/images/<filename>")
|
| 56 |
+
def serve_image(filename):
|
| 57 |
+
return send_from_directory("/tmp", filename)
|
| 58 |
+
|
| 59 |
+
base_url = os.getenv("SPACE_HOST")
|
| 60 |
+
###
|
| 61 |
+
|
| 62 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 63 |
app.logger.setLevel(logging.INFO)
|
| 64 |
|
|
|
|
| 117 |
dist_path = tempfile_path + '.' + ext
|
| 118 |
dist_name = os.path.basename(dist_path)
|
| 119 |
os.rename(tempfile_path, dist_path)
|
| 120 |
+
|
| 121 |
+
image_url = f"{base_url}/images{dist_path}"
|
| 122 |
|
| 123 |
with ApiClient(configuration) as api_client:
|
| 124 |
line_bot_api = MessagingApi(api_client)
|
|
|
|
| 127 |
reply_token=event.reply_token,
|
| 128 |
messages=[
|
| 129 |
TextMessage(text='Save content.'),
|
| 130 |
+
TextMessage(text=image_url)
|
| 131 |
# TextMessage(text=request.host_url + os.path.join('static', 'tmp', dist_name))
|
| 132 |
]
|
| 133 |
)
|