File size: 2,181 Bytes
f7fe4ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49b8283
f7fe4ff
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
import gradio as gr
import os
from database_helper import DatabaseIO
import pymongo
import re
from bson import ObjectId


def check_credentials(email, password):
    try:
        with DatabaseIO(collection_name=os.environ['MONGO_COLLECTION_INTERVIEWEES']) as db_io:
            user_collection = db_io.collection

            if not re.match(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$", email):
                print("Email is invalid")
                return False
            
            print("Email is valid")

            user = user_collection.find_one({"email": email})
            
            # If email is not found, create a new email
            if user is None:
                new_email = {"email": email}
                user_collection.insert_one(new_email)
                print("New email created")
                with DatabaseIO(collection_name=os.environ['MONGO_COLLECTION_STUDIES']) as db_io_study:
                    study_collection = db_io_study.collection
                    study_id = ObjectId(password)  # Convert the string to ObjectId
                    study = study_collection.find_one({"_id": study_id})
                    
                    if study is not None:
                        return True
                    else:
                        return False
            
            if user is not None:
                with DatabaseIO(collection_name=os.environ['MONGO_COLLECTION_STUDIES']) as db_io_study:
                    study_collection = db_io_study.collection
                    study_id = ObjectId(password)  # Convert the string to ObjectId
                    study = study_collection.find_one({"_id": study_id})
                    
                    if study is not None:
                        return True
                    else:
                        return False

    except pymongo.errors.ConnectionFailure as e:
        print(f"Could not connect to MongoDB: {e}")
        return False
    except Exception as e:
        print(f"Error occurred: {e}")
        return False

interview=gr.load("qualz/interview-beta", src="spaces",hf_token=os.environ["hf_token"])
interview.launch(auth=check_credentials)