CoolFace
Apppublic

cesmith012/technical-assistant

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
fixed_function.py66 linesDownload Raw Back to root
1from typing import List, Dict2from datetime import datetime3import pandas as pd4import tempfile5import traceback6import os7import json8import logging9from datasets import Dataset, concatenate_datasets, Features, Value10 11# Configure logger12logger = logging.getLogger(__name__)13 14class TechnicalAssistant:15    def __init__(self, hf_api=None, huggingface_token=None, memory_file="memory.json"):16        """Initialize the memory manager."""17        self.hf_api = hf_api18        self.huggingface_token = huggingface_token19        self.memory_file = memory_file20        self.memory = []21        self._load_json_memory()22 23    def _format_sources(self, sources: List[Dict] = None) -> str:24        if sources:25            return ", ".join([f"{source['source']} ({source['type']})" for source in sources])26        return ""27 28    def _load_json_memory(self):29        # Implementation of _load_json_memory method30        pass31 32    def _save_json_memory(self, json_data):33        # Implementation of _save_json_memory method34        pass35 36    async def store_interaction(self, question, response, model, context=None):37        """Store an interaction in memory."""38        try:39            # Create interaction record40            interaction = {41                "question": question,42                "response": response,43                "model": model,44                "timestamp": datetime.now().isoformat(),45                "context": context46            }47            48            # Add to memory49            self.memory.append(interaction)50            51            # Save to JSON file52            self._save_json_memory()53            54            # If Hugging Face API is configured, also save to dataset55            if self.hf_api and self.huggingface_token:56                try:57                    dataset = self.hf_api.load_dataset("technical-assistant/memory")58                    dataset = dataset.add_item(interaction)59                    dataset.push_to_hub("technical-assistant/memory", token=self.huggingface_token)60                except Exception as e:61                    logger.error(f"Error saving to Hugging Face: {str(e)}")62            63            return True64        except Exception as e:65            logger.error(f"Error storing interaction: {str(e)}")66            return False