Spaces:
Sleeping
Sleeping
Upload Langchain.py
Browse files- pages/Langchain.py +99 -0
pages/Langchain.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import datetime
|
3 |
+
import sqlite3
|
4 |
+
import asyncio
|
5 |
+
import requests
|
6 |
+
import conteneiro
|
7 |
+
import websockets
|
8 |
+
import streamlit as st
|
9 |
+
from agentLangchain import langchainAgent
|
10 |
+
from Classifier import IntentClassifier
|
11 |
+
|
12 |
+
servers = []
|
13 |
+
clients = []
|
14 |
+
inputs = []
|
15 |
+
outputs = []
|
16 |
+
messagess = []
|
17 |
+
intentios = []
|
18 |
+
used_ports = []
|
19 |
+
server_ports = []
|
20 |
+
client_ports = []
|
21 |
+
|
22 |
+
db = sqlite3.connect('chat-hub.db')
|
23 |
+
cursor = db.cursor()
|
24 |
+
cursor.execute('CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY AUTOINCREMENT, sender TEXT, message TEXT, timestamp TEXT)')
|
25 |
+
db.commit()
|
26 |
+
|
27 |
+
async def main():
|
28 |
+
|
29 |
+
userInput = st.chat_input("Ask Agent")
|
30 |
+
|
31 |
+
c1, c2 = st.columns(2)
|
32 |
+
fireAPI = st.text_input("Fireworks API")
|
33 |
+
|
34 |
+
with c1:
|
35 |
+
stat1 = st.empty()
|
36 |
+
state1 = stat1.status(label="Langchain", state="complete", expanded=False)
|
37 |
+
state1.write(conteneiro.servers)
|
38 |
+
websocketPort = st.number_input("Websocket servers", min_value=1000, max_value=9999, value=1000)
|
39 |
+
startServer = st.button("Start server")
|
40 |
+
stopServer = st.button("Stop server")
|
41 |
+
|
42 |
+
with c2:
|
43 |
+
stat2 = st.empty()
|
44 |
+
state2 = stat2.status(label="Langchain", state="complete", expanded=False)
|
45 |
+
state2.write(conteneiro.clients)
|
46 |
+
clientPort = st.number_input("Websocket clients", min_value=1000, max_value=9999, value=1000)
|
47 |
+
start_Client = st.button("Start client")
|
48 |
+
stopClient = st.button("Stop client")
|
49 |
+
|
50 |
+
with st.sidebar:
|
51 |
+
cont = st.empty()
|
52 |
+
status = cont.status(label="Langchain", state="complete", expanded=False)
|
53 |
+
|
54 |
+
|
55 |
+
if userInput:
|
56 |
+
user_input = st.chat_message("human")
|
57 |
+
user_input.markdown(userInput)
|
58 |
+
messagess.append(user_input)
|
59 |
+
agent = langchainAgent(fireAPI)
|
60 |
+
response = await agent.askQuestion(userInput)
|
61 |
+
outputMsg = st.chat_message("ai")
|
62 |
+
outputMsg.markdown(response)
|
63 |
+
await agent.handleInput(response)
|
64 |
+
|
65 |
+
if start_Client:
|
66 |
+
voiceCli = f"Langchain client port: {clientPort}"
|
67 |
+
stat2.empty()
|
68 |
+
state2 = stat2.status(label=voiceCli, state="running", expanded=True)
|
69 |
+
state2.write(conteneiro.clients)
|
70 |
+
cont.empty()
|
71 |
+
status = cont.status(label=voiceCli, state="running", expanded=True)
|
72 |
+
status.write(conteneiro.servers)
|
73 |
+
try:
|
74 |
+
client = langchainAgent(fireAPI)
|
75 |
+
await client.startClient(clientPort)
|
76 |
+
print(f"Connecting client on port {clientPort}...")
|
77 |
+
await asyncio.Future()
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
print(f"Error: {e}")
|
81 |
+
|
82 |
+
if startServer:
|
83 |
+
vooiceSrv = f"Langchain server pport: {websocketPort}"
|
84 |
+
stat1.empty()
|
85 |
+
state1 = stat1.status(label=vooiceSrv, state="running", expanded=False)
|
86 |
+
state1.write(conteneiro.clients)
|
87 |
+
cont.empty()
|
88 |
+
status = cont.status(label=vooiceSrv, state="running", expanded=False)
|
89 |
+
status.write(conteneiro.clients)
|
90 |
+
try:
|
91 |
+
server = langchainAgent(fireAPI)
|
92 |
+
await server.start_server(websocketPort)
|
93 |
+
print(f"Starting WebSocket server on port {websocketPort}...")
|
94 |
+
await asyncio.Future()
|
95 |
+
|
96 |
+
except Exception as e:
|
97 |
+
print(f"Error: {e}")
|
98 |
+
|
99 |
+
asyncio.run(main())
|