Futuresony commited on
Commit
5117938
·
verified ·
1 Parent(s): 4a85dca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -34
app.py CHANGED
@@ -5,59 +5,147 @@ import os
5
  # Initialize the Flask app
6
  app = Flask(__name__)
7
 
8
- # Initialize the Hugging Face Inference Client (Replace with your actual model identifier)
9
  client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
10
 
11
- # Parameters from the image
12
- MAX_TOKENS = 1520
13
- TEMPERATURE = 0.7
14
- TOP_P = 0.95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- # In-memory storage for active chats (to maintain chat history)
17
- chat_history = {}
18
 
19
- @app.route("/")
20
- def home():
21
- return render_template("editor.html")
 
22
 
23
- @app.route("/generate_code", methods=["POST"])
24
- def generate_code():
25
- # Get the user ID (or session) and the prompt
26
- user_id = request.json.get("user_id")
27
- prompt = request.json.get("prompt")
28
 
29
- # Get chat history for the user or initialize it
30
- if user_id not in chat_history:
31
- chat_history[user_id] = []
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- # Append the user's prompt to the chat history
34
- chat_history[user_id].append({"role": "user", "content": prompt})
 
 
 
 
 
 
 
35
 
36
- # System message
 
 
 
 
 
 
 
 
 
 
37
  system_message = "You are a friendly chatbot."
38
 
39
- # Build the messages for the model
40
  messages = [{"role": "system", "content": system_message}]
41
- messages.extend(chat_history[user_id]) # Add previous conversation history
42
 
43
- # Generate the response
44
- generated_code = ""
45
  for msg in client.chat_completion(
46
  messages=messages,
47
- max_tokens=MAX_TOKENS,
48
- temperature=TEMPERATURE,
49
- top_p=TOP_P,
50
  stream=True,
 
 
51
  ):
52
  token = msg.choices[0].delta.content
53
- generated_code += token
54
-
55
- # Save the assistant's response to the chat history
56
- chat_history[user_id].append({"role": "assistant", "content": generated_code})
57
 
58
- return jsonify({"code": generated_code})
 
 
59
 
60
  if __name__ == "__main__":
61
  # Use PORT environment variable or default to 7860
62
  port = int(os.getenv("PORT", 7860))
63
- app.run(host="0.0.0.0", port=port)
 
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)