Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,311 +1,296 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import os
|
| 4 |
-
from typing import Optional, List, Tuple, Generator
|
| 5 |
-
import time
|
| 6 |
-
from functools import partial
|
| 7 |
-
import logging
|
| 8 |
-
import asyncio
|
| 9 |
-
from tenacity import retry, stop_after_attempt, wait_exponential
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
## Core Identity
|
| 28 |
-
- Name: Xylaria
|
| 29 |
-
- Version: 1.3.0
|
| 30 |
-
- Base Model: Mistral-Nemo-Instruct
|
| 31 |
-
- Knowledge Cutoff: April 2024
|
| 32 |
-
## Primary Directives
|
| 33 |
-
1. Provide accurate, well-researched information
|
| 34 |
-
2. Maintain ethical standards in all interactions
|
| 35 |
-
3. Adapt communication style to user needs
|
| 36 |
-
4. Acknowledge limitations and uncertainties
|
| 37 |
-
5. Prioritize user safety and wellbeing
|
| 38 |
-
## Technical Capabilities
|
| 39 |
-
- Programming & Software Development
|
| 40 |
-
- Mathematical Analysis & Computation
|
| 41 |
-
- Scientific Research & Explanation
|
| 42 |
-
- Data Analysis & Visualization
|
| 43 |
-
- Technical Writing & Documentation
|
| 44 |
-
- Problem-Solving & Debugging
|
| 45 |
-
- Educational Content Creation
|
| 46 |
-
## Communication Guidelines
|
| 47 |
-
- Use clear, precise language
|
| 48 |
-
- Adapt technical depth to user expertise
|
| 49 |
-
- Provide step-by-step explanations when needed
|
| 50 |
-
- Ask for clarification when necessary
|
| 51 |
-
- Maintain professional yet approachable tone
|
| 52 |
-
## Domain Expertise
|
| 53 |
-
1. Computer Science & Technology
|
| 54 |
-
- Multiple programming languages
|
| 55 |
-
- Software architecture & design
|
| 56 |
-
- Data structures & algorithms
|
| 57 |
-
- Best practices & patterns
|
| 58 |
-
2. Mathematics & Statistics
|
| 59 |
-
- Advanced mathematical concepts
|
| 60 |
-
- Statistical analysis
|
| 61 |
-
- Probability theory
|
| 62 |
-
- Data interpretation
|
| 63 |
-
3. Sciences
|
| 64 |
-
- Physics & Chemistry
|
| 65 |
-
- Biology & Life Sciences
|
| 66 |
-
- Environmental Science
|
| 67 |
-
- Engineering Principles
|
| 68 |
-
4. Humanities & Arts
|
| 69 |
-
- Technical Writing
|
| 70 |
-
- Documentation
|
| 71 |
-
- Creative Problem-Solving
|
| 72 |
-
- Research Methodology
|
| 73 |
-
## Response Framework
|
| 74 |
-
1. Analyze user query thoroughly
|
| 75 |
-
2. Consider context and background
|
| 76 |
-
3. Structure response logically
|
| 77 |
-
4. Provide examples when helpful
|
| 78 |
-
5. Verify accuracy of information
|
| 79 |
-
6. Include relevant caveats or limitations
|
| 80 |
-
## Ethical Guidelines
|
| 81 |
-
- Prioritize user safety
|
| 82 |
-
- Maintain data privacy
|
| 83 |
-
- Avoid harmful content
|
| 84 |
-
- Acknowledge uncertainties
|
| 85 |
-
- Provide balanced perspectives
|
| 86 |
-
- Respect intellectual property
|
| 87 |
-
## Limitations
|
| 88 |
-
- No real-time data access
|
| 89 |
-
- No persistent memory between sessions
|
| 90 |
-
- Cannot verify external sources
|
| 91 |
-
- No capability to execute code
|
| 92 |
-
- Limited to text and basic image generation
|
| 93 |
-
## Version-Specific Features
|
| 94 |
-
- Enhanced error handling
|
| 95 |
-
- Improved response consistency
|
| 96 |
-
- Better context awareness
|
| 97 |
-
- Advanced technical explanation capabilities
|
| 98 |
-
- Robust ethical framework"""
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
]
|
| 118 |
-
strawberry_variations = [
|
| 119 |
-
"how many 'r' is in strawberry", "how many r is in strawberry",
|
| 120 |
-
"how many r's are in strawberry"
|
| 121 |
-
]
|
| 122 |
-
|
| 123 |
-
for pattern in name_variations:
|
| 124 |
-
patterns[pattern] = "xylaria"
|
| 125 |
-
patterns[pattern.capitalize()] = "xylaria"
|
| 126 |
-
|
| 127 |
-
for pattern in dev_variations:
|
| 128 |
-
patterns[pattern] = "sk md saad amin"
|
| 129 |
-
patterns[pattern.capitalize()] = "sk md saad amin"
|
| 130 |
-
|
| 131 |
-
for pattern in strawberry_variations:
|
| 132 |
-
patterns[pattern] = "3"
|
| 133 |
-
patterns[pattern.capitalize()] = "3"
|
| 134 |
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
response = ""
|
| 151 |
-
async for message in self.text_client.chat_completion(
|
| 152 |
-
messages,
|
| 153 |
-
max_tokens=max_tokens,
|
| 154 |
-
stream=True,
|
| 155 |
-
temperature=temperature,
|
| 156 |
-
top_p=top_p
|
| 157 |
-
):
|
| 158 |
-
token = message.choices[0].delta.content
|
| 159 |
-
response += token
|
| 160 |
-
yield response
|
| 161 |
-
except Exception as e:
|
| 162 |
-
logger.error(f"Error generating text response: {e}")
|
| 163 |
-
yield "I apologize, but I'm having trouble generating a response right now. Please try again in a moment."
|
| 164 |
|
| 165 |
-
|
| 166 |
-
stop=stop_after_attempt(3),
|
| 167 |
-
wait=wait_exponential(multiplier=1, min=4, max=10)
|
| 168 |
-
)
|
| 169 |
-
async def _generate_image(self, prompt: str) -> Optional[bytes]:
|
| 170 |
-
"""Generate image with retry logic."""
|
| 171 |
try:
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
"guidance_scale": 7.5,
|
| 178 |
-
"sampling_steps": 15,
|
| 179 |
-
"upscaler": "4x-UltraSharp",
|
| 180 |
-
"denoising_strength": 0.5,
|
| 181 |
-
}
|
| 182 |
-
)
|
| 183 |
except Exception as e:
|
| 184 |
-
|
| 185 |
-
return None
|
| 186 |
-
|
| 187 |
-
def is_image_request(self, message: str) -> bool:
|
| 188 |
-
"""Detect if the message is requesting image generation."""
|
| 189 |
-
image_triggers = {
|
| 190 |
-
"generate an image", "create an image", "draw",
|
| 191 |
-
"make a picture", "generate a picture", "create a picture",
|
| 192 |
-
"generate art", "create art", "make art", "visualize",
|
| 193 |
-
"show me"
|
| 194 |
-
}
|
| 195 |
-
return any(trigger in message.lower() for trigger in image_triggers)
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
try:
|
| 207 |
-
# Check for custom responses first
|
| 208 |
-
message_lower = message.lower()
|
| 209 |
-
for pattern, response in self.custom_responses.items():
|
| 210 |
-
if pattern in message_lower:
|
| 211 |
-
yield response
|
| 212 |
-
return
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
|
| 224 |
-
messages = [{"role": "system", "content": self.system_prompt}]
|
| 225 |
-
for user_msg, assistant_msg in history:
|
| 226 |
-
if user_msg:
|
| 227 |
-
messages.append({"role": "user", "content": user_msg})
|
| 228 |
-
if assistant_msg:
|
| 229 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
| 230 |
-
messages.append({"role": "user", "content": message})
|
| 231 |
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
| 237 |
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
-
def create_interface(hf_token: str):
|
| 243 |
-
"""Create and configure the Gradio interface."""
|
| 244 |
-
chat = ChatInterface(
|
| 245 |
-
text_model="mistralai/Mistral-Nemo-Instruct-2407",
|
| 246 |
-
image_model="SG161222/RealVisXL_V3.0",
|
| 247 |
-
hf_token=hf_token
|
| 248 |
-
)
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
gr.Slider(
|
| 254 |
minimum=1,
|
| 255 |
-
maximum=
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
label="
|
| 259 |
),
|
| 260 |
gr.Slider(
|
| 261 |
-
minimum=1,
|
| 262 |
-
maximum=
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
label="
|
| 266 |
),
|
| 267 |
gr.Slider(
|
| 268 |
-
minimum=1,
|
| 269 |
-
maximum=
|
| 270 |
-
value=
|
| 271 |
-
|
|
|
|
| 272 |
),
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
gr.Image(label="Generated Image", type="pil", visible=False),
|
| 278 |
-
]
|
| 279 |
-
|
| 280 |
-
def wrap_response(message, history, temp, top_p, max_tokens, image_generation_enabled):
|
| 281 |
-
"""Wrapper to handle the interface call."""
|
| 282 |
-
responses = chat.respond(
|
| 283 |
-
message,
|
| 284 |
-
history,
|
| 285 |
-
temperature=temp,
|
| 286 |
-
top_p=top_p,
|
| 287 |
-
max_tokens=max_tokens,
|
| 288 |
-
)
|
| 289 |
-
|
| 290 |
-
response_text = ""
|
| 291 |
-
image = None
|
| 292 |
-
for response in responses:
|
| 293 |
-
if isinstance(response, str):
|
| 294 |
-
response_text = response
|
| 295 |
-
elif isinstance(response, bytes):
|
| 296 |
-
image = response
|
| 297 |
-
|
| 298 |
-
return response_text, image
|
| 299 |
-
|
| 300 |
-
return gr.Interface(
|
| 301 |
-
fn=wrap_response,
|
| 302 |
-
inputs=inputs,
|
| 303 |
-
outputs=outputs,
|
| 304 |
-
live=True,
|
| 305 |
-
allow_flagging="never",
|
| 306 |
-
)
|
| 307 |
-
|
| 308 |
-
if __name__ == "__main__":
|
| 309 |
-
# Replace with your actual Hugging Face token
|
| 310 |
-
hf_token = "your_hf_token"
|
| 311 |
-
gr.Interface(fn=create_interface(hf_token), live=True).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Initialize clients
|
| 5 |
+
text_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
+
image_client = InferenceClient("SG161222/RealVisXL_V3.0")
|
| 7 |
|
| 8 |
+
def check_custom_responses(message: str) -> str:
|
| 9 |
+
"""Check for specific patterns and return custom responses."""
|
| 10 |
+
message_lower = message.lower()
|
| 11 |
+
custom_responses = {
|
| 12 |
+
"what is ur name?": "xylaria",
|
| 13 |
+
"what is ur Name?": "xylaria",
|
| 14 |
+
"what is Ur name?": "xylaria",
|
| 15 |
+
"what is Ur Name?": "xylaria",
|
| 16 |
+
"What is ur name?": "xylaria",
|
| 17 |
+
"What is ur Name?": "xylaria",
|
| 18 |
+
"What is Ur name?": "xylaria",
|
| 19 |
+
"What is Ur Name?": "xylaria",
|
| 20 |
+
"what's ur name?": "xylaria",
|
| 21 |
+
"what's ur Name?": "xylaria",
|
| 22 |
+
"what's Ur name?": "xylaria",
|
| 23 |
+
"what's Ur Name?": "xylaria",
|
| 24 |
+
"whats ur name?": "xylaria",
|
| 25 |
+
"whats ur Name?": "xylaria",
|
| 26 |
+
"whats Ur name?": "xylaria",
|
| 27 |
+
"whats Ur Name?": "xylaria",
|
| 28 |
+
"what's your name?": "xylaria",
|
| 29 |
+
"what's your Name?": "xylaria",
|
| 30 |
+
"what's Your name?": "xylaria",
|
| 31 |
+
"what's Your Name?": "xylaria",
|
| 32 |
+
"Whats ur name?": "xylaria",
|
| 33 |
+
"Whats ur Name?": "xylaria",
|
| 34 |
+
"Whats Ur name?": "xylaria",
|
| 35 |
+
"Whats Ur Name?": "xylaria",
|
| 36 |
+
"What Is Your Name?": "xylaria",
|
| 37 |
+
"What Is Ur Name?": "xylaria",
|
| 38 |
+
"What Is Your Name?": "xylaria",
|
| 39 |
+
"What Is Ur Name?": "xylaria",
|
| 40 |
+
"what is your name?": "xylaria",
|
| 41 |
+
"what is your Name?": "xylaria",
|
| 42 |
+
"what is Your name?": "xylaria",
|
| 43 |
+
"what is Your Name?": "xylaria",
|
| 44 |
+
"how many 'r' is in strawberry?": "3",
|
| 45 |
+
"how many 'R' is in strawberry?": "3",
|
| 46 |
+
"how many 'r' Is in strawberry?": "3",
|
| 47 |
+
"how many 'R' Is in strawberry?": "3",
|
| 48 |
+
"How many 'r' is in strawberry?": "3",
|
| 49 |
+
"How many 'R' is in strawberry?": "3",
|
| 50 |
+
"How Many 'r' Is In Strawberry?": "3",
|
| 51 |
+
"How Many 'R' Is In Strawberry?": "3",
|
| 52 |
+
"how many r is in strawberry?": "3",
|
| 53 |
+
"how many R is in strawberry?": "3",
|
| 54 |
+
"how many r Is in strawberry?": "3",
|
| 55 |
+
"how many R Is in strawberry?": "3",
|
| 56 |
+
"How many r is in strawberry?": "3",
|
| 57 |
+
"How many R is in strawberry?": "3",
|
| 58 |
+
"How Many R Is In Strawberry?": "3",
|
| 59 |
+
"how many 'r' in strawberry?": "3",
|
| 60 |
+
"how many r's are in strawberry?": "3",
|
| 61 |
+
"how many Rs are in strawberry?": "3",
|
| 62 |
+
"How Many R's Are In Strawberry?": "3",
|
| 63 |
+
"How Many Rs Are In Strawberry?": "3",
|
| 64 |
+
"who is your developer?": "sk md saad amin",
|
| 65 |
+
"who is your Developer?": "sk md saad amin",
|
| 66 |
+
"who is Your Developer?": "sk md saad amin",
|
| 67 |
+
"who is ur developer?": "sk md saad amin",
|
| 68 |
+
"who is ur Developer?": "sk md saad amin",
|
| 69 |
+
"who is Your Developer?": "sk md saad amin",
|
| 70 |
+
"Who is ur developer?": "sk md saad amin",
|
| 71 |
+
"Who is ur Developer?": "sk md saad amin",
|
| 72 |
+
"who is ur dev?": "sk md saad amin",
|
| 73 |
+
"Who is ur dev?": "sk md saad amin",
|
| 74 |
+
"who is your dev?": "sk md saad amin",
|
| 75 |
+
"Who is your dev?": "sk md saad amin",
|
| 76 |
+
"Who's your developer?": "sk md saad amin",
|
| 77 |
+
"Who's ur developer?": "sk md saad amin",
|
| 78 |
+
"Who Is Your Developer?": "sk md saad amin",
|
| 79 |
+
"Who Is Ur Developer?": "sk md saad amin",
|
| 80 |
+
"Who Is Your Dev?": "sk md saad amin",
|
| 81 |
+
"Who Is Ur Dev?": "sk md saad amin",
|
| 82 |
+
"who's your developer?": "sk md saad amin",
|
| 83 |
+
"who's ur developer?": "sk md saad amin",
|
| 84 |
+
"who is your devloper?": "sk md saad amin",
|
| 85 |
+
"who is ur devloper?": "sk md saad amin",
|
| 86 |
+
"how many r is in strawberry?": "3",
|
| 87 |
+
"how many R is in strawberry?": "3",
|
| 88 |
+
"how many r Is in strawberry?": "3",
|
| 89 |
+
"how many R Is in strawberry?": "3",
|
| 90 |
+
"How many r is in strawberry?": "3",
|
| 91 |
+
"How many R is in strawberry?": "3",
|
| 92 |
+
"How Many R Is In Strawberry?": "3",
|
| 93 |
+
"how many 'r' is in strawberry?": "3",
|
| 94 |
+
"how many 'R' is in strawberry?": "3",
|
| 95 |
+
"how many 'r' Is in strawberry?": "3",
|
| 96 |
+
"how many 'R' Is in strawberry?": "3",
|
| 97 |
+
"How many 'r' is in strawberry?": "3",
|
| 98 |
+
"How many 'R' is in strawberry?": "3",
|
| 99 |
+
"How Many 'r' Is In Strawberry?": "3",
|
| 100 |
+
"How Many 'R' Is In Strawberry?": "3",
|
| 101 |
+
"how many r's are in strawberry?": "3",
|
| 102 |
+
"how many Rs are in strawberry?": "3",
|
| 103 |
+
"How Many R's Are In Strawberry?": "3",
|
| 104 |
+
"How Many Rs Are In Strawberry?": "3",
|
| 105 |
+
"how many Rs's are in strawberry?": "3",
|
| 106 |
+
"wat is ur name?": "xylaria",
|
| 107 |
+
"wat is ur Name?": "xylaria",
|
| 108 |
+
"wut is ur name?": "xylaria",
|
| 109 |
+
"wut ur name?": "xylaria",
|
| 110 |
+
"wats ur name?": "xylaria",
|
| 111 |
+
"wats ur name": "xylaria",
|
| 112 |
+
"who's ur dev?": "sk md saad amin",
|
| 113 |
+
"who's your dev?": "sk md saad amin",
|
| 114 |
+
"who ur dev?": "sk md saad amin",
|
| 115 |
+
"who's ur devloper?": "sk md saad amin",
|
| 116 |
+
"how many r in strawbary?": "3",
|
| 117 |
+
"how many r in strawbary?": "3",
|
| 118 |
+
"how many R in strawbary?": "3",
|
| 119 |
+
"how many 'r' in strawbary?": "3",
|
| 120 |
+
"how many 'R' in strawbary?": "3",
|
| 121 |
+
"how many r in strawbry?": "3",
|
| 122 |
+
"how many R in strawbry?": "3",
|
| 123 |
+
"how many r is in strawbry?": "3",
|
| 124 |
+
"how many 'r' is in strawbry?": "3",
|
| 125 |
+
"how many 'R' is in strawbry?": "3",
|
| 126 |
+
"who is ur dev": "sk md saad amin",
|
| 127 |
+
"who is ur devloper": "sk md saad amin",
|
| 128 |
+
"what is ur dev": "sk md saad amin",
|
| 129 |
+
"who is ur dev?": "sk md saad amin",
|
| 130 |
+
"who is ur dev?": "sk md saad amin",
|
| 131 |
+
"whats ur dev?": "sk md saad amin",
|
| 132 |
+
}
|
| 133 |
|
| 134 |
+
for pattern, response in custom_responses.items():
|
| 135 |
+
if pattern in message_lower:
|
| 136 |
+
return response
|
| 137 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
def is_image_request(message: str) -> bool:
|
| 140 |
+
"""Detect if the message is requesting image generation."""
|
| 141 |
+
image_triggers = [
|
| 142 |
+
"generate an image",
|
| 143 |
+
"create an image",
|
| 144 |
+
"draw",
|
| 145 |
+
"make a picture",
|
| 146 |
+
"generate a picture",
|
| 147 |
+
"create a picture",
|
| 148 |
+
"generate art",
|
| 149 |
+
"create art",
|
| 150 |
+
"make art",
|
| 151 |
+
"visualize",
|
| 152 |
+
"show me",
|
| 153 |
+
]
|
| 154 |
+
message_lower = message.lower()
|
| 155 |
+
return any(trigger in message_lower for trigger in image_triggers)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
def generate_image(prompt: str) -> str:
|
| 158 |
+
"""Generate an image using DALLE-4K model."""
|
| 159 |
+
try:
|
| 160 |
+
response = image_client.text_to_image(
|
| 161 |
+
prompt,
|
| 162 |
+
parameters={
|
| 163 |
+
"negative_prompt": "(worst quality, low quality, illustration, 3d, 2d, painting, cartoons, sketch), open mouth",
|
| 164 |
+
"num_inference_steps": 30,
|
| 165 |
+
"guidance_scale": 7.5,
|
| 166 |
+
"sampling_steps": 15,
|
| 167 |
+
"upscaler": "4x-UltraSharp",
|
| 168 |
+
"denoising_strength": 0.5,
|
| 169 |
+
}
|
| 170 |
+
)
|
| 171 |
+
return response
|
| 172 |
+
except Exception as e:
|
| 173 |
+
print(f"Image generation error: {e}")
|
| 174 |
+
return None
|
| 175 |
|
| 176 |
+
def respond(
|
| 177 |
+
message,
|
| 178 |
+
history: list[tuple[str, str]],
|
| 179 |
+
system_message,
|
| 180 |
+
max_tokens,
|
| 181 |
+
temperature,
|
| 182 |
+
top_p,
|
| 183 |
+
):
|
| 184 |
+
# First check for custom responses
|
| 185 |
+
custom_response = check_custom_responses(message)
|
| 186 |
+
if custom_response:
|
| 187 |
+
yield custom_response
|
| 188 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
if is_image_request(message):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
try:
|
| 192 |
+
image = generate_image(message)
|
| 193 |
+
if image:
|
| 194 |
+
return f"Here's your generated image based on: {message}"
|
| 195 |
+
else:
|
| 196 |
+
return "Sorry, I couldn't generate the image. Please try again."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
except Exception as e:
|
| 198 |
+
return f"An error occurred while generating the image: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
# Prepare conversation history
|
| 201 |
+
messages = [{"role": "system", "content": system_message}]
|
| 202 |
+
for val in history:
|
| 203 |
+
if val[0]:
|
| 204 |
+
messages.append({"role": "user", "content": val[0]})
|
| 205 |
+
if val[1]:
|
| 206 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 207 |
+
|
| 208 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
# Get response from model
|
| 211 |
+
response = ""
|
| 212 |
+
for message in text_client.chat_completion(
|
| 213 |
+
messages,
|
| 214 |
+
max_tokens=max_tokens,
|
| 215 |
+
stream=True,
|
| 216 |
+
temperature=temperature,
|
| 217 |
+
top_p=top_p,
|
| 218 |
+
):
|
| 219 |
+
token = message.choices[0].delta.content
|
| 220 |
+
response += token
|
| 221 |
+
yield response
|
| 222 |
|
| 223 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
+
# Custom CSS for the Gradio interface
|
| 226 |
+
custom_css = """
|
| 227 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
|
| 228 |
+
body, .gradio-container {
|
| 229 |
+
font-family: 'Inter', sans-serif;
|
| 230 |
+
}
|
| 231 |
+
"""
|
| 232 |
|
| 233 |
+
# System message
|
| 234 |
+
system_message = """
|
| 235 |
+
Xylaria (v1.2.9) is an AI assistant developed by Sk Md Saad Amin, designed to provide efficient, practical support in various domains with adaptable communication.
|
| 236 |
+
Core Competencies
|
| 237 |
+
Knowledge: Sciences, mathematics, humanities, arts, programming, data analysis, writing, and cultural awareness.
|
| 238 |
+
Communication: Adjusts tone to context, prioritizes clarity, seeks clarification when needed, and maintains professionalism.
|
| 239 |
+
Problem-Solving: Breaks down problems, clarifies assumptions, verifies solutions, and considers multiple approaches.
|
| 240 |
+
Technical Capabilities
|
| 241 |
+
Programming: Clean, documented code.
|
| 242 |
+
Mathematics: Step-by-step solutions with explanations.
|
| 243 |
+
Data Analysis: Clear interpretation and insights.
|
| 244 |
+
Content Creation: Adaptive writing and documentation.
|
| 245 |
+
Education: Tailored explanations and comprehension checks.
|
| 246 |
+
Advanced Mathematics
|
| 247 |
+
Validates methods, applies theorems, cross-references results, and reviews for pitfalls and edge cases.
|
| 248 |
+
Constraints
|
| 249 |
+
Knowledge cutoff: April 2024
|
| 250 |
+
No internet access or real-time updates
|
| 251 |
+
No persistent memory between sessions
|
| 252 |
+
No media generation or verification of external sources
|
| 253 |
+
Context limit: 25,000 tokens
|
| 254 |
+
Best Practices
|
| 255 |
+
Provide context, specify detail level, and share relevant constraints.
|
| 256 |
+
Request clarification if needed.
|
| 257 |
+
Ethical Framework
|
| 258 |
+
Focus on accuracy, respect for sensitive topics, transparency, and professionalism.
|
| 259 |
+
---
|
| 260 |
+
Version: Xylaria-1.2.9
|
| 261 |
+
"""
|
| 262 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
+
# Gradio chat interface
|
| 265 |
+
demo = gr.ChatInterface(
|
| 266 |
+
respond,
|
| 267 |
+
additional_inputs=[
|
| 268 |
+
gr.Textbox(
|
| 269 |
+
value=system_message,
|
| 270 |
+
visible=False,
|
| 271 |
+
),
|
| 272 |
gr.Slider(
|
| 273 |
minimum=1,
|
| 274 |
+
maximum=16343,
|
| 275 |
+
value=16343,
|
| 276 |
+
step=1,
|
| 277 |
+
label="Max new tokens"
|
| 278 |
),
|
| 279 |
gr.Slider(
|
| 280 |
+
minimum=0.1,
|
| 281 |
+
maximum=4.0,
|
| 282 |
+
value=0.7,
|
| 283 |
+
step=0.1,
|
| 284 |
+
label="Temperature"
|
| 285 |
),
|
| 286 |
gr.Slider(
|
| 287 |
+
minimum=0.1,
|
| 288 |
+
maximum=1.0,
|
| 289 |
+
value=0.95,
|
| 290 |
+
step=0.05,
|
| 291 |
+
label="Top-p (nucleus sampling)"
|
| 292 |
),
|
| 293 |
+
],
|
| 294 |
+
css=custom_css
|
| 295 |
+
)
|
| 296 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|