Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,97 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
""
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
if __name__ ==
|
| 64 |
-
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, render_template_string
|
| 2 |
+
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
|
| 6 |
+
# Mock database
|
| 7 |
+
posts = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# API to post an item
|
| 10 |
+
@app.route('/api/post', methods=['POST'])
|
| 11 |
+
def create_post():
|
| 12 |
+
data = request.json
|
| 13 |
+
post = {
|
| 14 |
+
"id": len(posts) + 1,
|
| 15 |
+
"title": data.get("title"),
|
| 16 |
+
"description": data.get("description"),
|
| 17 |
+
"price": data.get("price"),
|
| 18 |
+
"location": data.get("location"),
|
| 19 |
+
"timestamp": datetime.utcnow().isoformat()
|
| 20 |
+
}
|
| 21 |
+
posts.append(post)
|
| 22 |
+
return jsonify({"success": True, "post": post}), 201
|
| 23 |
|
| 24 |
+
# API to get posts by location
|
| 25 |
+
@app.route('/api/posts', methods=['GET'])
|
| 26 |
+
def get_posts():
|
| 27 |
+
location = request.args.get("location", "").lower()
|
| 28 |
+
filtered = [p for p in posts if location in p["location"].lower()]
|
| 29 |
+
return jsonify({"posts": filtered})
|
| 30 |
|
| 31 |
+
# HTML frontend
|
| 32 |
+
html_template = """
|
| 33 |
+
<!DOCTYPE html>
|
| 34 |
+
<html lang="th">
|
| 35 |
+
<head>
|
| 36 |
+
<meta charset="UTF-8">
|
| 37 |
+
<title>ตลาดข้างบ้าน</title>
|
| 38 |
+
</head>
|
| 39 |
+
<body>
|
| 40 |
+
<h2>📦 ลงขายของ</h2>
|
| 41 |
+
<form id="postForm">
|
| 42 |
+
<label>ชื่อสินค้า:</label><br>
|
| 43 |
+
<input type="text" id="title"><br>
|
| 44 |
+
<label>รายละเอียด:</label><br>
|
| 45 |
+
<input type="text" id="description"><br>
|
| 46 |
+
<label>ราคา (บาท):</label><br>
|
| 47 |
+
<input type="number" id="price"><br>
|
| 48 |
+
<label>สถานที่ (เช่น หอ A):</label><br>
|
| 49 |
+
<input type="text" id="location"><br><br>
|
| 50 |
+
<button type="submit">โพสต์เลย</button>
|
| 51 |
+
</form>
|
| 52 |
|
| 53 |
+
<h2>🔍 ค้นหาของใกล้คุณ</h2>
|
| 54 |
+
<input type="text" id="searchLocation" placeholder="ใส่ชื่อสถานที่">
|
| 55 |
+
<button onclick="searchPosts()">ค้นหา</button>
|
| 56 |
+
<div id="results"></div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
<script>
|
| 59 |
+
document.getElementById("postForm").onsubmit = async function(e) {
|
| 60 |
+
e.preventDefault();
|
| 61 |
+
const data = {
|
| 62 |
+
title: document.getElementById("title").value,
|
| 63 |
+
description: document.getElementById("description").value,
|
| 64 |
+
price: document.getElementById("price").value,
|
| 65 |
+
location: document.getElementById("location").value
|
| 66 |
+
};
|
| 67 |
+
const res = await fetch('/api/post', {
|
| 68 |
+
method: 'POST',
|
| 69 |
+
headers: { 'Content-Type': 'application/json' },
|
| 70 |
+
body: JSON.stringify(data)
|
| 71 |
+
});
|
| 72 |
+
const result = await res.json();
|
| 73 |
+
alert("โพสต์เรียบร้อยแล้ว!");
|
| 74 |
+
};
|
| 75 |
|
| 76 |
+
async function searchPosts() {
|
| 77 |
+
const location = document.getElementById("searchLocation").value;
|
| 78 |
+
const res = await fetch(`/api/posts?location=${location}`);
|
| 79 |
+
const result = await res.json();
|
| 80 |
+
let html = "<ul>";
|
| 81 |
+
result.posts.forEach(post => {
|
| 82 |
+
html += `<li><b>${post.title}</b> - ${post.price} บาท<br>${post.description}<br><i>${post.location}</i><hr></li>`;
|
| 83 |
+
});
|
| 84 |
+
html += "</ul>";
|
| 85 |
+
document.getElementById("results").innerHTML = html;
|
| 86 |
+
}
|
| 87 |
+
</script>
|
| 88 |
+
</body>
|
| 89 |
+
</html>
|
| 90 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
@app.route('/')
|
| 93 |
+
def index():
|
| 94 |
+
return render_template_string(html_template)
|
| 95 |
|
| 96 |
+
if __name__ == '__main__':
|
| 97 |
+
app.run(host='0.0.0.0', port=8080)
|