Spaces:
Configuration error

File size: 2,319 Bytes
03c3044
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
import json
from pymongo import MongoClient
from sentence_transformers import SentenceTransformer, util
import boto3
import os

#----------------------------------------------------------------------------
def lambda_handler(event, context):
    # S3'tan Python versiyonlarını alma
    python_versions = _known_python_versions()

    # Kullanıcıdan gelen inputu işleme
    user_input = event.get("input")
    if not user_input:
        return {
            "statusCode": 400,
            "body": json.dumps("Input data is required")
        }

    
    save_user_input_to_mongodb({"input": user_input})
    refenece_data_mongodb({})

    # Similarity hesaplama
    similarity_score = calculate_similarity(user_input, "Referans metin")

    # Sonuçları döndürme
    return {
        "statusCode": 200,
        "body": json.dumps({
            "python_versions": python_versions,
            "similarity_score": similarity_score
        })
    }
#-------------------------------------------------------------------------------
#s3 keywords'ü oluşturma gerekli
def _known_python_versions():
    """Get current list from S3."""
    try:
        s3 = boto3.resource("s3")
        obj = s3.Object(os.environ["S3_BUCKET"], "python/versions.json")
        content = obj.get()["Body"].read().decode("utf-8")
    except botocore.exceptions.ClientError:
        print("Key not found, using empty list")
        content = '{"python_versions":[]}'
    return json.loads(content)

def save_user_input_to_mongodb(input_data):
    client = MongoClient(os.environ["MONGODB_URI"])
    db = client["EgitimDatabase"]
    collection = db["input"]
    collection.insert_one(input_data)
    client.close()

def refenece_data_mongodb(reference_data):
    client = MongoClient(os.environ["MONGODB_URI"])
    db = client["EgitimDatabase"]
    collection = db["test"]
    collection.insert_one(reference_data)
    client.close()
#--------------------------------------------------------------------------------
def calculate_similarity(text1, text2):
    model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
    embedding1 = model.encode(text1, convert_to_tensor=True)
    embedding2 = model.encode(text2, convert_to_tensor=True)
    similarity_score = util.pytorch_cos_sim(embedding1, embedding2)
    return similarity_score.item()