Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,24 +1,66 @@
|
|
1 |
-
from flask import Flask, request
|
2 |
from twilio.twiml.messaging_response import MessagingResponse
|
3 |
-
|
4 |
-
sms_chain = LLMChain(
|
5 |
-
llm=Baseten(model="YOUR-MODEL-VERSION-ID"),
|
6 |
-
prompt=prompt,
|
7 |
-
memory=ConversationBufferWindowMemory(k=2),
|
8 |
-
llm_kwargs={"max_length": 4096}
|
9 |
-
)
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
@app.route("/sms", methods=['GET', 'POST'])
|
15 |
def sms():
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
return str(resp)
|
22 |
|
23 |
if __name__ == "__main__":
|
24 |
-
app.run(debug=True)
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
from twilio.twiml.messaging_response import MessagingResponse
|
3 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
# Initialize InferenceClient
|
8 |
+
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
|
9 |
+
conversation_history = {}
|
10 |
+
|
11 |
+
def format_prompt(message, history):
|
12 |
+
prompt = "<s>"
|
13 |
+
for user_prompt, bot_response in history:
|
14 |
+
prompt += f"[INST] {user_prompt} [/INST]"
|
15 |
+
prompt += f" {bot_response}</s> "
|
16 |
+
prompt += f"[INST] {message} [/INST]"
|
17 |
+
return prompt
|
18 |
+
|
19 |
+
def generate(prompt, history, temperature=0.9, max_new_tokens=100, top_p=0.95, repetition_penalty=1.0):
|
20 |
+
formatted_prompt = format_prompt(prompt, history)
|
21 |
+
|
22 |
+
generate_kwargs = dict(
|
23 |
+
temperature=temperature,
|
24 |
+
max_new_tokens=max_new_tokens,
|
25 |
+
top_p=top_p,
|
26 |
+
repetition_penalty=repetition_penalty,
|
27 |
+
do_sample=True,
|
28 |
+
seed=42,
|
29 |
+
)
|
30 |
+
|
31 |
+
response = client.text_generation(
|
32 |
+
formatted_prompt,
|
33 |
+
**generate_kwargs,
|
34 |
+
stream=True,
|
35 |
+
details=True,
|
36 |
+
return_full_text=False
|
37 |
+
)
|
38 |
+
|
39 |
+
output = ""
|
40 |
+
for token in response:
|
41 |
+
if hasattr(token, 'token') and hasattr(token.token, 'text'):
|
42 |
+
output += token.token.text
|
43 |
+
|
44 |
+
return output
|
45 |
|
46 |
@app.route("/sms", methods=['GET', 'POST'])
|
47 |
def sms():
|
48 |
+
try:
|
49 |
+
resp = MessagingResponse()
|
50 |
+
sender_number = request.form['From']
|
51 |
+
inb_msg = request.form['Body'].strip()
|
52 |
+
|
53 |
+
history = conversation_history.get(sender_number, [])
|
54 |
+
output = generate(inb_msg, history)
|
55 |
+
history.append((inb_msg, output))
|
56 |
+
conversation_history[sender_number] = history
|
57 |
+
|
58 |
+
resp.message(output)
|
59 |
+
except Exception as e:
|
60 |
+
print(f"Error processing SMS: {str(e)}")
|
61 |
+
resp.message("Sorry, an error occurred. Please try again later.")
|
62 |
+
|
63 |
return str(resp)
|
64 |
|
65 |
if __name__ == "__main__":
|
66 |
+
app.run(debug=True, port=5000)
|