ayush101NLPtask commited on
Commit
aab0504
·
verified ·
1 Parent(s): de85a37

Upload app2.py

Browse files
Files changed (1) hide show
  1. app2.py +123 -0
app2.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sqlite3
2
+ from hashlib import sha256
3
+ import streamlit as st
4
+ from openai import OpenAI
5
+ client = OpenAI()
6
+ # Step 1: Initialize the SQLite database
7
+ def init_db():
8
+ conn = sqlite3.connect('user_data.db') # Connect to SQLite database
9
+ c = conn.cursor()
10
+
11
+ # Create the users table if it doesn't exist
12
+ c.execute('''CREATE TABLE IF NOT EXISTS users (
13
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
14
+ username TEXT NOT NULL,
15
+ password TEXT NOT NULL)''')
16
+
17
+ # Create a table for storing user inputs
18
+ c.execute('''CREATE TABLE IF NOT EXISTS user_inputs (
19
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
20
+ username TEXT NOT NULL,
21
+ input1 TEXT NOT NULL,
22
+ input2 TEXT NOT NULL,
23
+ input3 TEXT NOT NULL)''')
24
+
25
+ conn.commit()
26
+ return conn, c
27
+
28
+ # Step 2: Hash passwords for secure storage
29
+ def hash_password(password):
30
+ return sha256(password.encode()).hexdigest()
31
+
32
+ # Step 3: Verify if the username and password match any record in the database
33
+ def verify_login(username, password, c):
34
+ hashed_pw = hash_password(password)
35
+ c.execute('SELECT * FROM users WHERE username=? AND password=?', (username, hashed_pw))
36
+ return c.fetchone()
37
+
38
+ # Step 4: Create a new user (for initial setup)
39
+ def create_user(username, password, conn, c):
40
+ hashed_pw = hash_password(password)
41
+ c.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, hashed_pw))
42
+ conn.commit()
43
+
44
+ # Step 5: Insert the user inputs into the database
45
+ def insert_user_inputs(username, input1, input2, input3, conn, c):
46
+ c.execute('INSERT INTO user_inputs (username, input1, input2, input3) VALUES (?, ?, ?, ?)',
47
+ (username, input1, input2, input3))
48
+ conn.commit()
49
+ def generate(input1,input2,input3):
50
+ prompt = f"Using these three inputs: '{input1}' and '{input2}' and {input3}, first input we received from customner 2nd input we have to send also consider input 3 for morte accurate message write a mail which is more empathy based, formalized for business use, should address the issue well, positive tonality and dont make any assumption just write the mail"
51
+ completion = client.chat.completions.create(
52
+ model="gpt-4o-mini",
53
+ messages=[
54
+ {"role": "system", "content": "You are a helpful assistant who writes mail formally, and grammatically corrected."},
55
+ {
56
+ "role": "user",
57
+ "content": prompt
58
+ }
59
+ ]
60
+ )
61
+ return completion.choices[0].message.content
62
+ # Initialize the database and cursor
63
+ conn, c = init_db()
64
+
65
+ # Create a session state to store login status
66
+ if 'logged_in' not in st.session_state:
67
+ st.session_state['logged_in'] = False
68
+ if 'username' not in st.session_state:
69
+ st.session_state['username'] = None
70
+
71
+ # Streamlit UI: Login Page
72
+ if not st.session_state['logged_in']:
73
+ st.title("User Login")
74
+
75
+ # Username and password inputs
76
+ username = st.text_input("Username")
77
+ password = st.text_input("Password", type="password")
78
+
79
+ # Login button
80
+ if st.button("Login"):
81
+ if username and password:
82
+ user = verify_login(username, password, c)
83
+ if user:
84
+ st.session_state['logged_in'] = True
85
+ st.session_state['username'] = username
86
+ st.success(f"Welcome, {username}! You have successfully logged in.")
87
+ else:
88
+ st.error("Invalid username or password. Please try again.")
89
+ else:
90
+ st.error("Please enter both username and password.")
91
+ else:
92
+ # When the user is logged in, display the new section
93
+ st.title(f"Welcome {st.session_state['username']}! Writing AI at your assitance")
94
+
95
+ # Input fields
96
+ input1 = st.text_input("Mail received from Customer")
97
+ input2 = st.text_input("Mail EARC send to customer")
98
+ input3 = st.text_input("Any input")
99
+
100
+ # Submit inputs button
101
+ if st.button("Submit Inputs"):
102
+ if input1 and input2 and input3:
103
+ insert_user_inputs(st.session_state['username'], input1, input2, input3, conn, c)
104
+ output = generate(input1, input2, input3)
105
+
106
+ st.success(output)
107
+ else:
108
+ st.error("Please fill in all the fields.")
109
+
110
+ # (Optional) Add a section for creating new users, useful for testing
111
+ st.subheader("Create a new account (for testing)")
112
+ new_username = st.text_input("New Username")
113
+ new_password = st.text_input("New Password", type="password")
114
+
115
+ if st.button("Create Account"):
116
+ if new_username and new_password:
117
+ create_user(new_username, new_password, conn, c)
118
+ st.success(f"Account created successfully for {new_username}!")
119
+ else:
120
+ st.error("Please enter both a username and a password.")
121
+
122
+ # Close the database connection when done
123
+ conn.close()