Futuresony commited on
Commit
8c8e15f
·
verified ·
1 Parent(s): 0cbb769

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -22
app.py CHANGED
@@ -1,46 +1,152 @@
1
  from flask import Flask, request, jsonify, render_template
2
  from huggingface_hub import InferenceClient
 
3
 
 
4
  app = Flask(__name__)
 
 
5
  client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- @app.route("/")
9
- def index():
10
- return render_template("index.html")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  @app.route("/respond", methods=["POST"])
14
  def respond():
 
15
  data = request.json
16
- message = data["message"]
17
- history = data["history"]
18
- system_message = data["system_message"]
19
- max_tokens = data["max_tokens"]
20
- temperature = data["temperature"]
21
- top_p = data["top_p"]
22
 
 
 
 
 
23
  messages = [{"role": "system", "content": system_message}]
24
- for val in history:
25
- if val[0]:
26
- messages.append({"role": "user", "content": val[0]})
27
- if val[1]:
28
- messages.append({"role": "assistant", "content": val[1]})
29
  messages.append({"role": "user", "content": message})
30
 
 
31
  response = ""
32
- for message in client.chat_completion(
33
- messages,
34
- max_tokens=max_tokens,
35
  stream=True,
36
- temperature=temperature,
37
- top_p=top_p,
38
  ):
39
- token = message.choices[0].delta.content
40
  response += token
41
 
 
 
42
  return jsonify({"response": response})
43
 
44
-
45
  if __name__ == "__main__":
46
- app.run(debug=True)
 
 
 
 
1
  from flask import Flask, request, jsonify, render_template
2
  from huggingface_hub import InferenceClient
3
+ import os
4
 
5
+ # Initialize the Flask app
6
  app = Flask(__name__)
7
+
8
+ # Initialize the Hugging Face Inference Client
9
  client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
10
 
11
+ # HTML template for the app
12
+ HTML_TEMPLATE = """
13
+ <!DOCTYPE html>
14
+ <html lang="en">
15
+ <head>
16
+ <meta charset="UTF-8">
17
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
18
+ <title>Chat Interface</title>
19
+ <style>
20
+ body {
21
+ font-family: Arial, sans-serif;
22
+ margin: 20px;
23
+ }
24
+ .container {
25
+ max-width: 800px;
26
+ margin: auto;
27
+ }
28
+ .chat-box {
29
+ border: 1px solid #ccc;
30
+ padding: 10px;
31
+ border-radius: 5px;
32
+ background-color: #f9f9f9;
33
+ height: 400px;
34
+ overflow-y: auto;
35
+ }
36
+ .chat-input {
37
+ width: 100%;
38
+ padding: 10px;
39
+ margin-top: 10px;
40
+ border: 1px solid #ccc;
41
+ border-radius: 5px;
42
+ }
43
+ .response {
44
+ background-color: #e8f5e9;
45
+ border: 1px solid #81c784;
46
+ padding: 8px;
47
+ margin-top: 5px;
48
+ border-radius: 5px;
49
+ white-space: pre-wrap; /* Preserve formatting */
50
+ }
51
+ .bold-italic {
52
+ font-weight: bold;
53
+ font-style: italic;
54
+ }
55
+ .code-box {
56
+ background-color: #f4f4f4;
57
+ border: 1px solid #ddd;
58
+ padding: 10px;
59
+ font-family: monospace;
60
+ white-space: pre-wrap;
61
+ overflow-x: auto;
62
+ border-radius: 5px;
63
+ }
64
+ </style>
65
+ </head>
66
+ <body>
67
+ <div class="container">
68
+ <h1>Chat Interface</h1>
69
+ <div class="chat-box" id="chat-box">
70
+ <!-- Chat history will appear here -->
71
+ </div>
72
+ <textarea id="message" class="chat-input" placeholder="Type your message here..."></textarea>
73
+ <button onclick="sendMessage()">Send</button>
74
+ </div>
75
+ <script>
76
+ function sendMessage() {
77
+ const message = document.getElementById("message").value;
78
+ const chatBox = document.getElementById("chat-box");
79
 
80
+ if (!message.trim()) return; // Don't send empty messages
81
+
82
+ // Append the user's message to the chat box
83
+ const userMessage = document.createElement("div");
84
+ userMessage.textContent = "You: " + message;
85
+ chatBox.appendChild(userMessage);
86
+
87
+ // Clear the input field
88
+ document.getElementById("message").value = "";
89
+
90
+ // Send the message to the server
91
+ fetch("/respond", {
92
+ method: "POST",
93
+ headers: {
94
+ "Content-Type": "application/json",
95
+ },
96
+ body: JSON.stringify({ message: message }),
97
+ })
98
+ .then((response) => response.json())
99
+ .then((data) => {
100
+ // Append the assistant's response to the chat box
101
+ const botMessage = document.createElement("div");
102
+ botMessage.innerHTML = `<div class="response">${data.response}</div>`;
103
+ chatBox.appendChild(botMessage);
104
 
105
+ // Scroll to the bottom of the chat box
106
+ chatBox.scrollTop = chatBox.scrollHeight;
107
+ })
108
+ .catch((error) => console.error("Error:", error));
109
+ }
110
+ </script>
111
+ </body>
112
+ </html>
113
+ """
114
+
115
+ @app.route("/")
116
+ def home():
117
+ return HTML_TEMPLATE
118
 
119
  @app.route("/respond", methods=["POST"])
120
  def respond():
121
+ # Extract data from the request
122
  data = request.json
123
+ message = data.get("message", "")
 
 
 
 
 
124
 
125
+ # Define system message
126
+ system_message = "You are a friendly chatbot."
127
+
128
+ # Build the chat history and message
129
  messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
130
  messages.append({"role": "user", "content": message})
131
 
132
+ # Call the Hugging Face API
133
  response = ""
134
+ for msg in client.chat_completion(
135
+ messages=messages,
136
+ max_tokens=512,
137
  stream=True,
138
+ temperature=0.7,
139
+ top_p=0.95,
140
  ):
141
+ token = msg.choices[0].delta.content
142
  response += token
143
 
144
+ # Return the response in bold italic format
145
+ response = response.replace("**", '<span class="bold-italic">').replace("**", "</span>")
146
  return jsonify({"response": response})
147
 
 
148
  if __name__ == "__main__":
149
+ # Use PORT environment variable or default to 7860
150
+ port = int(os.getenv("PORT", 7860))
151
+ app.run(host="0.0.0.0", port=port)
152
+