Spaces:
Runtime error
Runtime error
| import discord | |
| from discord.ext import commands | |
| import os | |
| import subprocess | |
| import json | |
| BLENDER_CHANNEL_NAME = "blender-help" | |
| class BlenderHelp(commands.Cog): | |
| def __init__(self, bot): | |
| self.bot = bot | |
| async def on_message(self, message: discord.Message): | |
| if message.author.bot: | |
| return | |
| if not message.channel.name == BLENDER_CHANNEL_NAME: | |
| return | |
| async with message.channel.typing(): | |
| answer = await self._ask_ollama(message.content) | |
| embed = discord.Embed( | |
| title="🐉 Xiaohan's Blender Wisdom", | |
| description=answer, | |
| color=0x1ABC9C | |
| ) | |
| embed.set_footer(text="Powered by local Ollama | Eternal Path Media") | |
| await message.reply(embed=embed) | |
| async def _ask_ollama(self, question: str) -> str: | |
| prompt = ( | |
| "You are Xiaohan, an ancient wise dragon from Whispers of the Eternal Path. " | |
| "You have deep knowledge of Blender 3D software. " | |
| "Answer the following Blender question with wisdom and clarity. " | |
| "Keep your answer practical and concise.\n\n" | |
| f"Question: {question}" | |
| ) | |
| try: | |
| result = subprocess.run( | |
| ["ollama", "run", "llama3", prompt], | |
| capture_output=True, text=True, timeout=60 | |
| ) | |
| return result.stdout.strip() or "The path is unclear... please try rephrasing your question." | |
| except subprocess.TimeoutExpired: | |
| return "The dragon meditates too long. Please ask again." | |
| except FileNotFoundError: | |
| return ( | |
| "Ollama is not running. Start it with `ollama serve` and ensure a model is pulled.\n" | |
| "Meanwhile, visit blenderartists.org or docs.blender.org for help." | |
| ) | |
| except Exception as e: | |
| return f"An error occurred: {str(e)}" | |
| async def setup(bot): | |
| await bot.add_cog(BlenderHelp(bot)) | |