make it so that this ai trains along the way please while use prompts. no tensorflow or…
- User
make it so that this ai trains along the way please while use prompts. no tensorflow or torch
import discord import requests import json import os from datetime import datetime import asyncio
Directory for conversation logs
DATA_DIR = "conversation_logs" MAX_RECORDS_PER_FILE = 100 # Max interactions per JSON file os.makedirs(DATA_DIR, exist_ok=True)
Discord Bot Token (replace with your actual token securely)
DISCORD_TOKEN = 'YOUR_DISCORD_TOKEN'
Hugging Face API settings
HF_API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium" HF_API_TOKEN = "YOUR_HF_API_TOKEN" # Replace with your Hugging Face token headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
Initialize Discord client
client = discord.Client(intents=discord.Intents.default()) processing_lock = asyncio.Lock() # Prevent duplicate messages by using a lock
def get_log_file(): """Retrieve the latest log file or create a new one if the latest is full.""" files = sorted([f for f in os.listdir(DATA_DIR) if f.endswith('.json')]) if files: latest_file = files[-1] with open(os.path.join(DATA_DIR, latest_file), 'r') as f: data = json.load(f) if len(data) < MAX_RECORDS_PER_FILE: return os.path.join(DATA_DIR, latest_file)
# Create a new log file if none exists or if the latest is full new_file = f"log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json" with open(os.path.join(DATA_DIR, new_file), 'w') as f: json.dump([], f) return os.path.join(DATA_DIR, new_file)def log_interaction(user_message, bot_response): """Log user and bot interactions into JSON for future training.""" log_file = get_log_file() with open(log_file, 'r+') as f: data = json.load(f) data.append({ "user_message": user_message, "bot_response": bot_response, "timestamp": datetime.now().isoformat() }) f.seek(0) json.dump(data, f, indent=4)
def generate_response(user_message): """Generate a response using the Hugging Face Inference API.""" response = requests.post( HF_API_URL, headers=headers, json={"inputs": user_message}, ) if response.status_code == 200: return response.json()[0]['generated_text'] else: return "I'm having trouble generating a response right now."
@client.event async def on_ready(): print(f'Logged in as {client.user}')
@client.event async def on_message(message): # Ignore the bot's own messages if message.author == client.user: return
# Check if the bot was mentioned if client.user in message.mentions: # Acquire the lock to prevent duplicate responses async with processing_lock: # Remove the bot mention from the message content user_message = message.content.replace(f'<@{client.user.id}>', '').strip() # Send a "Processing" message processing_message = await message.channel.send("Processing message...") # Generate a response using the Hugging Face Inference API bot_response = generate_response(user_message) # Log interaction to JSON for future training log_interaction(user_message, bot_response) # Delete the "Processing" message and send the final response in an embed await processing_message.delete() # Create an embed for the AI response embed = discord.Embed(title="AI Response", color=discord.Color.blue()) embed.add_field(name="User Message", value=user_message, inline=False) embed.add_field(name="AI Response", value=bot_response, inline=False) embed.set_footer(text=f"Requested by {message.author.name}", icon_url=message.author.avatar.url) await message.channel.send(embed=embed) - Assistant
- User
make the script
- Assistant