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