File size: 4,781 Bytes
844ea56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import requests
import datetime
import http.server
import websockets
import asyncio
import sqlite3
import json
import tensorflow as tf
import gradio as gr

from bs4 import BeautifulSoup

# Define a placeholder function that doesn't do anything
def placeholder_fn(input_text):
    pass

# Set up the HTTP server
class SimpleHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            with open('index.html', 'rb') as file:
                self.wfile.write(file.read())
        else:
            self.send_response(404)
            self.end_headers()

# Define the function for handling incoming messages
async def handleMessage(message):
    response = {'message': message.get('message')}
    try:
        question = message.get('message')
        result = await askQuestion(question)
        response['result'] = result
    except Exception as e:
        print(e)
    return response

# Define the function for sending an error message
def sendErrorMessage(ws, errorMessage):
    errorResponse = {'error': errorMessage}
    ws.send(json.dumps(errorResponse))

# Define the function for asking a question to the chatbot
async def askQuestion(question):
    try:
        db = sqlite3.connect('chat-hub.db')
        cursor = db.execute('SELECT * FROM messages ORDER BY timestamp DESC LIMIT 10')
        messages = cursor.fetchall()
        pastUserInputs = []
        generatedResponses = []
        for i, message in enumerate(messages):
            if i % 2 == 0:
                pastUserInputs.append(message[2])
            else:
                generatedResponses.append(message[2])
        response = requests.post(
            "https://api-inference.huggingface.co/models/facebook/blenderbot-400M-distill",
            headers={
                "Content-Type": "application/json",
                "Authorization": "Bearer HF_TOKEN",
            },
            json={
                "inputs": {
                    "text": question,
                    "past_user_inputs": pastUserInputs,
                    "generated_responses": generatedResponses,
                },
                "full_source": False,
            },
        )
        responseJson = response.json()
        outputText = responseJson["generated_text"]
        return outputText
    except Exception as e:
        print(e)

# Define the WebSocket handler
async def handleWebSocket(ws, path):
    print('New connection')
    try:
        # Set up the SQLite database
        db = sqlite3.connect('chat-hub.db')
        db.execute('CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, sender TEXT, message TEXT, timestamp TEXT)')
        db.commit()
        await ws.send('Hello! Please integrate yourself with the local sql database and file system')
        async for message in ws:
            print(f'Received message: {message}')            
            parsedMessage = json.loads(message)
            messageText = parsedMessage.get('text', '')
            timestamp = datetime.datetime.now().isoformat()
            sender = 'client'
            db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)', (sender, messageText, timestamp))
            db.commit()
            try:
                if 'text' in parsedMessage:
                    answer = await askQuestion(parsedMessage['text'])
                    response = {'answer': answer}
                    await ws.send(json.dumps(response))                    
                    serverMessageText = response.get('answer', '')
                    serverSender = 'server'
                    db.execute('INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)', (serverSender, serverMessageText, timestamp))
                    db.commit()
            except Exception as e:
                print(e)
                sendErrorMessage(ws, 'An error occurred while processing the message.')
    except websockets.exceptions.ConnectionClosedError as e:
        print(f"Connection closed: {e}")
    except Exception as e:
        print(f"Error: {e}")
    finally:
        print("Closing connection")

port=5000      
# Start the WebSocket server 
async def start_websockets():
    await(websockets.serve(handleWebSocket, 'localhost', port))
    print(f"Starting WebSocket server on port {port}...")

with gr.Blocks() as demo:

    # Define Gradio interface
    fn=placeholder_fn,  # Placeholder function
    inputs=[gr.Textbox()],
    outputs=[gr.Textbox()],
    startWebsockets = gr.Button("start Websocket Server")
    startWebsockets.click(start_websockets)
    live=True

demo.launch(server_port=8888)