import firebase_admin from firebase_admin import firestore import streamlit as st from datetime import datetime, timezone import uuid def load_ratings(db, models): try: ratings_ref = db.collection('ratings').document('current') ratings_doc = ratings_ref.get() if ratings_doc.exists: ratings = ratings_doc.to_dict() else: ratings = {model: 1000 for model in models} save_ratings(db, ratings) return ratings except Exception as e: st.error(f"Ошибка при загрузке рейтингов: {e}") return {model: 1000 for model in models} def save_ratings(db, ratings): try: ratings_ref = db.collection('ratings').document('current') ratings_ref.set(ratings) except Exception as e: st.error(f"Ошибка при сохранении рейтингов: {e}") def save_vote(db, winner, models): ratings = load_ratings(db, models) model_a = st.session_state.model_a model_b = st.session_state.model_b Ra = ratings[model_a] Rb = ratings[model_b] Sa, Sb = (1, 0) if winner == 'left' else (0, 1) Ea = 1 / (1 + 10 ** ((Rb - Ra) / 400)) Eb = 1 / (1 + 10 ** ((Ra - Rb) / 400)) K = 32 Ra_new = Ra + K * (Sa - Ea) Rb_new = Rb + K * (Sb - Eb) ratings[model_a] = Ra_new ratings[model_b] = Rb_new save_ratings(db, ratings) try: vote_data = { 'session_id': str(uuid.uuid4()), 'timestamp': datetime.now(timezone.utc), 'original_review': st.session_state.original_review, 'model_a': model_a, 'model_b': model_b, 'rewrite_a': st.session_state.rewrite_a, 'rewrite_b': st.session_state.rewrite_b, 'winner': winner } db.collection('votes').add(vote_data) except Exception as e: st.error(f"Ошибка при сохранении голосования: {e}")