Spaces:
Runtime error
Runtime error
Arnasltlt
commited on
Commit
•
07b1304
1
Parent(s):
40809b1
Add application and gradio
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
|
5 |
+
def send_message_via_api(to_number, message):
|
6 |
+
# response.json()API endpoint
|
7 |
+
url = "https://whatsapp.arnasltlt.repl.co/send"
|
8 |
+
print(to_number)
|
9 |
+
# Send POST request
|
10 |
+
response = requests.post(url, data={"to_number": to_number, "message": "Hi, What is the order status for " + message+ '?'})
|
11 |
+
print(response.text)
|
12 |
+
# Return the response or any relevant message
|
13 |
+
history = get_history()
|
14 |
+
return history
|
15 |
+
|
16 |
+
|
17 |
+
def get_history():
|
18 |
+
# Endpoint
|
19 |
+
url = "https://whatsapp.arnasltlt.repl.co/get_history"
|
20 |
+
|
21 |
+
# Make a GET request
|
22 |
+
response = requests.get(url)
|
23 |
+
|
24 |
+
# Ensure the response was successful
|
25 |
+
response.raise_for_status()
|
26 |
+
|
27 |
+
html_string = """
|
28 |
+
<html>
|
29 |
+
<head>
|
30 |
+
<style>
|
31 |
+
body {
|
32 |
+
font-family: Arial, sans-serif;
|
33 |
+
background-color: #f0f0f0;
|
34 |
+
margin: 0;
|
35 |
+
padding: 20px;
|
36 |
+
color: black;
|
37 |
+
}
|
38 |
+
.chat-container {
|
39 |
+
display: flex;
|
40 |
+
flex-direction: column;
|
41 |
+
max-width: 400px;
|
42 |
+
margin: 0 auto;
|
43 |
+
background-color: #fff;
|
44 |
+
border-radius: 8px;
|
45 |
+
padding: 10px;
|
46 |
+
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
47 |
+
}
|
48 |
+
.chat-bubble {
|
49 |
+
padding: 10px;
|
50 |
+
margin: 5px 0;
|
51 |
+
border-radius: 15px;
|
52 |
+
line-height: 1.4;
|
53 |
+
color: black;
|
54 |
+
}
|
55 |
+
.chat-bubble.assistant {
|
56 |
+
background-color: #009688;
|
57 |
+
align-self: flex-start;
|
58 |
+
}
|
59 |
+
.chat-bubble.user {
|
60 |
+
background-color: #ff5722;
|
61 |
+
align-self: flex-end;
|
62 |
+
}
|
63 |
+
#my_ai {
|
64 |
+
background-color: #2b524e;
|
65 |
+
padding:10px;
|
66 |
+
}
|
67 |
+
#your_ai {
|
68 |
+
background-color: #c7674a;
|
69 |
+
padding:10px;
|
70 |
+
}
|
71 |
+
#your_ai_box{
|
72 |
+
background:#c7674a;
|
73 |
+
}
|
74 |
+
#my_ai_box{
|
75 |
+
background:#2b524e;
|
76 |
+
border-color:black;
|
77 |
+
}
|
78 |
+
</style>
|
79 |
+
</head>
|
80 |
+
<body>
|
81 |
+
<div class="chat-container">
|
82 |
+
"""
|
83 |
+
history = response.json()
|
84 |
+
|
85 |
+
for entry in history:
|
86 |
+
role = entry['role']
|
87 |
+
content = entry['content']
|
88 |
+
html_string += f'<div class="chat-bubble {role}"><b>{role.capitalize()}:</b> {content}</div>'
|
89 |
+
|
90 |
+
html_string += """
|
91 |
+
</div>
|
92 |
+
</body>
|
93 |
+
</html>
|
94 |
+
"""
|
95 |
+
|
96 |
+
return html_string
|
97 |
+
|
98 |
+
def final_answer():
|
99 |
+
url = "https://whatsapp.arnasltlt.repl.co/get_order_status"
|
100 |
+
# Make a GET request
|
101 |
+
response = requests.get(url)
|
102 |
+
|
103 |
+
return response.text
|
104 |
+
|
105 |
+
|
106 |
+
with gr.Blocks() as demo:
|
107 |
+
with gr.Row():
|
108 |
+
with gr.Column():
|
109 |
+
inp=gr.Textbox(label='phone',value='+37068995284')
|
110 |
+
number= gr.Textbox(label='Order Number',value='134JAN42')
|
111 |
+
btn = gr.Button('Send')
|
112 |
+
with gr.Column():
|
113 |
+
gr.Markdown('### The conversation')
|
114 |
+
dt = gr.HTML(label="History")
|
115 |
+
demo.load(get_history, inputs=None, outputs=dt, every=4, queue=True)
|
116 |
+
with gr.Row():
|
117 |
+
final = gr.Textbox()
|
118 |
+
demo.load(final_answer, inputs=None, outputs=final, every=4, queue=True)
|
119 |
+
|
120 |
+
|
121 |
+
btn.click(fn=send_message_via_api,inputs=[inp,number],outputs=dt)
|
122 |
+
|
123 |
+
demo.queue(max_size=20)
|
124 |
+
demo.launch()
|