Spaces:
Runtime error
Runtime error
| from fastapi import FastAPI | |
| import sqlite3 | |
| from hashlib import sha256 | |
| import streamlit as st | |
| from openai import OpenAI | |
| app =FastAPI() | |
| client = OpenAI() | |
| # Step 1: Initialize the SQLite database | |
| def init_db(): | |
| conn = sqlite3.connect('user_data.db') # Connect to SQLite database | |
| c = conn.cursor() | |
| # Create the users table if it doesn't exist | |
| c.execute('''CREATE TABLE IF NOT EXISTS users ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| username TEXT NOT NULL, | |
| password TEXT NOT NULL)''') | |
| # Create a table for storing user inputs | |
| c.execute('''CREATE TABLE IF NOT EXISTS user_inputs ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| username TEXT NOT NULL, | |
| input1 TEXT NOT NULL, | |
| input2 TEXT NOT NULL, | |
| input3 TEXT NOT NULL)''') | |
| conn.commit() | |
| return conn, c | |
| # Step 2: Hash passwords for secure storage | |
| def hash_password(password): | |
| return sha256(password.encode()).hexdigest() | |
| # Step 3: Verify if the username and password match any record in the database | |
| def verify_login(username, password, c): | |
| hashed_pw = hash_password(password) | |
| c.execute('SELECT * FROM users WHERE username=? AND password=?', (username, hashed_pw)) | |
| return c.fetchone() | |
| # Step 4: Create a new user (for initial setup) | |
| def create_user(username, password, conn, c): | |
| hashed_pw = hash_password(password) | |
| c.execute('INSERT INTO users (username, password) VALUES (?, ?)', (username, hashed_pw)) | |
| conn.commit() | |
| # Step 5: Insert the user inputs into the database | |
| def insert_user_inputs(username, input1, input2, input3, conn, c): | |
| c.execute('INSERT INTO user_inputs (username, input1, input2, input3) VALUES (?, ?, ?, ?)', | |
| (username, input1, input2, input3)) | |
| conn.commit() | |
| def generate(input1,input2,input3): | |
| 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" | |
| completion = client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful assistant who writes mail formally, and grammatically corrected."}, | |
| { | |
| "role": "user", | |
| "content": prompt | |
| } | |
| ] | |
| ) | |
| return completion.choices[0].message.content | |
| # Initialize the database and cursor | |
| conn, c = init_db() | |
| # Create a session state to store login status | |
| if 'logged_in' not in st.session_state: | |
| st.session_state['logged_in'] = False | |
| if 'username' not in st.session_state: | |
| st.session_state['username'] = None | |
| # Streamlit UI: Login Page | |
| if not st.session_state['logged_in']: | |
| st.title("User Login") | |
| # Username and password inputs | |
| username = st.text_input("Username") | |
| password = st.text_input("Password", type="password") | |
| # Login button | |
| if st.button("Login"): | |
| if username and password: | |
| user = verify_login(username, password, c) | |
| if user: | |
| st.session_state['logged_in'] = True | |
| st.session_state['username'] = username | |
| st.success(f"Welcome, {username}! You have successfully logged in.") | |
| else: | |
| st.error("Invalid username or password. Please try again.") | |
| else: | |
| st.error("Please enter both username and password.") | |
| else: | |
| # When the user is logged in, display the new section | |
| st.title(f"Welcome {st.session_state['username']}! Writing AI at your assitance") | |
| # Input fields | |
| input1 = st.text_input("Mail received from Customer") | |
| input2 = st.text_input("Mail EARC send to customer") | |
| input3 = st.text_input("Any input") | |
| # Submit inputs button | |
| if st.button("Submit Inputs"): | |
| if input1 and input2 and input3: | |
| insert_user_inputs(st.session_state['username'], input1, input2, input3, conn, c) | |
| output = generate(input1, input2, input3) | |
| st.success(output) | |
| else: | |
| st.error("Please fill in all the fields.") | |
| # (Optional) Add a section for creating new users, useful for testing | |
| st.subheader("Create a new account (for testing)") | |
| new_username = st.text_input("New Username") | |
| new_password = st.text_input("New Password", type="password") | |
| if st.button("Create Account"): | |
| if new_username and new_password: | |
| create_user(new_username, new_password, conn, c) | |
| st.success(f"Account created successfully for {new_username}!") | |
| else: | |
| st.error("Please enter both a username and a password.") | |
| # Close the database connection when done | |
| conn.close() |