Spaces:
Configuration error
Configuration error
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() | |