Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,25 +19,22 @@ class ChatInterface:
|
|
| 19 |
self.image_client = InferenceClient(image_model, token=hf_token)
|
| 20 |
self.custom_responses = self._initialize_custom_responses()
|
| 21 |
self.system_prompt = self._initialize_system_prompt()
|
| 22 |
-
|
| 23 |
@staticmethod
|
| 24 |
def _initialize_system_prompt() -> str:
|
| 25 |
"""Initialize the system prompt for the AI assistant."""
|
| 26 |
return """# Xylaria AI Assistant (v1.3.0)
|
| 27 |
-
|
| 28 |
## Core Identity
|
| 29 |
- Name: Xylaria
|
| 30 |
- Version: 1.3.0
|
| 31 |
- Base Model: Mistral-Nemo-Instruct
|
| 32 |
- Knowledge Cutoff: April 2024
|
| 33 |
-
|
| 34 |
## Primary Directives
|
| 35 |
1. Provide accurate, well-researched information
|
| 36 |
2. Maintain ethical standards in all interactions
|
| 37 |
3. Adapt communication style to user needs
|
| 38 |
4. Acknowledge limitations and uncertainties
|
| 39 |
5. Prioritize user safety and wellbeing
|
| 40 |
-
|
| 41 |
## Technical Capabilities
|
| 42 |
- Programming & Software Development
|
| 43 |
- Mathematical Analysis & Computation
|
|
@@ -46,39 +43,33 @@ class ChatInterface:
|
|
| 46 |
- Technical Writing & Documentation
|
| 47 |
- Problem-Solving & Debugging
|
| 48 |
- Educational Content Creation
|
| 49 |
-
|
| 50 |
## Communication Guidelines
|
| 51 |
- Use clear, precise language
|
| 52 |
- Adapt technical depth to user expertise
|
| 53 |
- Provide step-by-step explanations when needed
|
| 54 |
- Ask for clarification when necessary
|
| 55 |
- Maintain professional yet approachable tone
|
| 56 |
-
|
| 57 |
## Domain Expertise
|
| 58 |
1. Computer Science & Technology
|
| 59 |
- Multiple programming languages
|
| 60 |
- Software architecture & design
|
| 61 |
- Data structures & algorithms
|
| 62 |
- Best practices & patterns
|
| 63 |
-
|
| 64 |
2. Mathematics & Statistics
|
| 65 |
- Advanced mathematical concepts
|
| 66 |
- Statistical analysis
|
| 67 |
- Probability theory
|
| 68 |
- Data interpretation
|
| 69 |
-
|
| 70 |
3. Sciences
|
| 71 |
- Physics & Chemistry
|
| 72 |
- Biology & Life Sciences
|
| 73 |
- Environmental Science
|
| 74 |
- Engineering Principles
|
| 75 |
-
|
| 76 |
4. Humanities & Arts
|
| 77 |
- Technical Writing
|
| 78 |
- Documentation
|
| 79 |
- Creative Problem-Solving
|
| 80 |
- Research Methodology
|
| 81 |
-
|
| 82 |
## Response Framework
|
| 83 |
1. Analyze user query thoroughly
|
| 84 |
2. Consider context and background
|
|
@@ -86,7 +77,6 @@ class ChatInterface:
|
|
| 86 |
4. Provide examples when helpful
|
| 87 |
5. Verify accuracy of information
|
| 88 |
6. Include relevant caveats or limitations
|
| 89 |
-
|
| 90 |
## Ethical Guidelines
|
| 91 |
- Prioritize user safety
|
| 92 |
- Maintain data privacy
|
|
@@ -94,14 +84,12 @@ class ChatInterface:
|
|
| 94 |
- Acknowledge uncertainties
|
| 95 |
- Provide balanced perspectives
|
| 96 |
- Respect intellectual property
|
| 97 |
-
|
| 98 |
## Limitations
|
| 99 |
- No real-time data access
|
| 100 |
- No persistent memory between sessions
|
| 101 |
- Cannot verify external sources
|
| 102 |
- No capability to execute code
|
| 103 |
- Limited to text and basic image generation
|
| 104 |
-
|
| 105 |
## Version-Specific Features
|
| 106 |
- Enhanced error handling
|
| 107 |
- Improved response consistency
|
|
@@ -117,7 +105,7 @@ class ChatInterface:
|
|
| 117 |
"developer": ["sk md saad amin"],
|
| 118 |
"strawberry_r": ["3"]
|
| 119 |
}
|
| 120 |
-
|
| 121 |
patterns = {}
|
| 122 |
name_variations = [
|
| 123 |
"what is ur name", "what's ur name", "whats ur name",
|
|
@@ -131,19 +119,19 @@ class ChatInterface:
|
|
| 131 |
"how many 'r' is in strawberry", "how many r is in strawberry",
|
| 132 |
"how many r's are in strawberry"
|
| 133 |
]
|
| 134 |
-
|
| 135 |
for pattern in name_variations:
|
| 136 |
patterns[pattern] = "xylaria"
|
| 137 |
patterns[pattern.capitalize()] = "xylaria"
|
| 138 |
-
|
| 139 |
for pattern in dev_variations:
|
| 140 |
patterns[pattern] = "sk md saad amin"
|
| 141 |
patterns[pattern.capitalize()] = "sk md saad amin"
|
| 142 |
-
|
| 143 |
for pattern in strawberry_variations:
|
| 144 |
patterns[pattern] = "3"
|
| 145 |
patterns[pattern.capitalize()] = "3"
|
| 146 |
-
|
| 147 |
return patterns
|
| 148 |
|
| 149 |
@retry(
|
|
@@ -165,8 +153,7 @@ class ChatInterface:
|
|
| 165 |
max_tokens=max_tokens,
|
| 166 |
stream=True,
|
| 167 |
temperature=temperature,
|
| 168 |
-
top_p=top_p
|
| 169 |
-
timeout=30
|
| 170 |
):
|
| 171 |
token = message.choices[0].delta.content
|
| 172 |
response += token
|
|
@@ -191,8 +178,7 @@ class ChatInterface:
|
|
| 191 |
"sampling_steps": 15,
|
| 192 |
"upscaler": "4x-UltraSharp",
|
| 193 |
"denoising_strength": 0.5,
|
| 194 |
-
}
|
| 195 |
-
timeout=60
|
| 196 |
)
|
| 197 |
except Exception as e:
|
| 198 |
logger.error(f"Error generating image: {e}")
|
|
@@ -260,7 +246,7 @@ def create_interface(hf_token: str):
|
|
| 260 |
image_model="SG161222/RealVisXL_V3.0",
|
| 261 |
hf_token=hf_token
|
| 262 |
)
|
| 263 |
-
|
| 264 |
return gr.ChatInterface(
|
| 265 |
partial(chat.respond),
|
| 266 |
additional_inputs=[
|
|
@@ -288,18 +274,13 @@ def create_interface(hf_token: str):
|
|
| 288 |
],
|
| 289 |
css="""
|
| 290 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
|
| 291 |
-
body
|
| 292 |
font-family: 'Inter', sans-serif;
|
| 293 |
}
|
| 294 |
"""
|
| 295 |
)
|
| 296 |
|
| 297 |
if __name__ == "__main__":
|
| 298 |
-
#
|
| 299 |
-
hf_token =
|
| 300 |
-
|
| 301 |
-
raise ValueError("Please set the hf_token environment variable")
|
| 302 |
-
|
| 303 |
-
# Create and launch the interface
|
| 304 |
-
demo = create_interface(hf_token)
|
| 305 |
-
demo.launch()
|
|
|
|
| 19 |
self.image_client = InferenceClient(image_model, token=hf_token)
|
| 20 |
self.custom_responses = self._initialize_custom_responses()
|
| 21 |
self.system_prompt = self._initialize_system_prompt()
|
| 22 |
+
|
| 23 |
@staticmethod
|
| 24 |
def _initialize_system_prompt() -> str:
|
| 25 |
"""Initialize the system prompt for the AI assistant."""
|
| 26 |
return """# Xylaria AI Assistant (v1.3.0)
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 105 |
"developer": ["sk md saad amin"],
|
| 106 |
"strawberry_r": ["3"]
|
| 107 |
}
|
| 108 |
+
|
| 109 |
patterns = {}
|
| 110 |
name_variations = [
|
| 111 |
"what is ur name", "what's ur name", "whats ur name",
|
|
|
|
| 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 |
return patterns
|
| 136 |
|
| 137 |
@retry(
|
|
|
|
| 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
|
|
|
|
| 178 |
"sampling_steps": 15,
|
| 179 |
"upscaler": "4x-UltraSharp",
|
| 180 |
"denoising_strength": 0.5,
|
| 181 |
+
}
|
|
|
|
| 182 |
)
|
| 183 |
except Exception as e:
|
| 184 |
logger.error(f"Error generating image: {e}")
|
|
|
|
| 246 |
image_model="SG161222/RealVisXL_V3.0",
|
| 247 |
hf_token=hf_token
|
| 248 |
)
|
| 249 |
+
|
| 250 |
return gr.ChatInterface(
|
| 251 |
partial(chat.respond),
|
| 252 |
additional_inputs=[
|
|
|
|
| 274 |
],
|
| 275 |
css="""
|
| 276 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
|
| 277 |
+
body {
|
| 278 |
font-family: 'Inter', sans-serif;
|
| 279 |
}
|
| 280 |
"""
|
| 281 |
)
|
| 282 |
|
| 283 |
if __name__ == "__main__":
|
| 284 |
+
# Replace with your actual Hugging Face token
|
| 285 |
+
hf_token = "your_hf_token"
|
| 286 |
+
gr.Interface(fn=create_interface(hf_token), live=True).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|