Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import requests | |
| def send_message_via_api(to_number, message): | |
| # response.json()API endpoint | |
| url = "https://whatsappv2.arnasltlt.repl.co/send" | |
| print(to_number) | |
| # Send POST request | |
| response = requests.post(url, data={"to_number": to_number, "message": f" {message}"}) | |
| print(response.text) | |
| # Return the response or any relevant message | |
| to_number = to_number.replace('+', '') | |
| history = get_history(to_number) | |
| return history | |
| def get_history(phone_number): | |
| # Endpoint | |
| to_number = phone_number.replace('+', '') | |
| print(to_number) | |
| url = f"https://whatsappv2.arnasltlt.repl.co/get_history?phone_number={to_number}" | |
| # Make a GET request | |
| response = requests.get(url) | |
| # Ensure the response was successful | |
| response.raise_for_status() | |
| html_string = """ | |
| <html> | |
| <head> | |
| <style> | |
| body { | |
| font-family: Arial, sans-serif; | |
| background-color: #f0f0f0; | |
| margin: 0; | |
| padding: 20px; | |
| color: black; | |
| } | |
| .chat-container { | |
| display: flex; | |
| flex-direction: column; | |
| max-width: 400px; | |
| margin: 0 auto; | |
| background-color: #fff; | |
| border-radius: 8px; | |
| padding: 10px; | |
| box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); | |
| } | |
| .chat-bubble { | |
| padding: 10px; | |
| margin: 5px 0; | |
| border-radius: 15px; | |
| line-height: 1.4; | |
| color: black; | |
| } | |
| .chat-bubble.assistant { | |
| background-color: #009688; | |
| align-self: flex-start; | |
| } | |
| .chat-bubble.function { | |
| background-color: gray; | |
| align-self: flex-start; | |
| } | |
| .chat-bubble.user { | |
| background-color: #ff5722; | |
| align-self: flex-end; | |
| } | |
| #my_ai { | |
| background-color: #2b524e; | |
| padding:10px; | |
| } | |
| #your_ai { | |
| background-color: #c7674a; | |
| padding:10px; | |
| } | |
| #your_ai_box{ | |
| background:#c7674a; | |
| } | |
| #my_ai_box{ | |
| background:#2b524e; | |
| border-color:black; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="chat-container"> | |
| """ | |
| history = response.json() | |
| for entry in history: | |
| role = entry[0] | |
| message = entry[1] | |
| html_string += f'<div class="chat-bubble {role}"><b>role: {role}</b> {message}</div>' | |
| html_string += """ | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| return html_string | |
| # def final_answer(): | |
| # url = "https://whatsapp.arnasltlt.repl.co/get_order_status" | |
| # # Make a GET request | |
| # response = requests.get(url) | |
| # | |
| # return response.text | |
| with gr.Blocks() as demo: | |
| with gr.Row(): | |
| with gr.Column(): | |
| inp=gr.Textbox(label='phone',value='+37068995284') | |
| number= gr.Textbox(label='Objective',value='Find out the order status of 134JAN42') | |
| btn = gr.Button('Send') | |
| # gr.Markdown('### The conversation') | |
| # dt = gr.HTML(label="History") | |
| # demo.load(get_history, inputs=inp, outputs=dt, every=5, queue=True) | |
| with gr.Column(): | |
| outcome = gr.Textbox(label='Outcome') | |
| # with gr.Row(): | |
| # final = gr.Textbox() | |
| # demo.load(final_answer, inputs=None, outputs=final, every=300, queue=True) | |
| btn.click(fn=send_message_via_api,inputs=[inp,number]) | |
| demo.queue(max_size=20) | |
| demo.launch() | |