Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
-
import asyncio
|
| 5 |
-
import os
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
-
app = FastAPI(title="AI Model Runner", version="1.0.0")
|
| 9 |
|
| 10 |
# Enable CORS
|
| 11 |
app.add_middleware(
|
|
@@ -16,57 +14,17 @@ app.add_middleware(
|
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
-
class ChatRequest(BaseModel):
|
| 20 |
-
message: str
|
| 21 |
-
history: list = []
|
| 22 |
-
|
| 23 |
-
class CodeRequest(BaseModel):
|
| 24 |
-
code: str
|
| 25 |
-
task: str = "explain"
|
| 26 |
-
|
| 27 |
-
class ReasoningRequest(BaseModel):
|
| 28 |
-
problem: str
|
| 29 |
-
context: str = ""
|
| 30 |
-
|
| 31 |
-
class SentimentRequest(BaseModel):
|
| 32 |
-
text: str
|
| 33 |
-
|
| 34 |
class AgenticRequest(BaseModel):
|
| 35 |
task: str
|
| 36 |
context: str = ""
|
| 37 |
-
available_tools: list = None
|
| 38 |
-
|
| 39 |
-
class ToolDefinition(BaseModel):
|
| 40 |
-
name: str
|
| 41 |
-
description: str
|
| 42 |
-
parameters: dict
|
| 43 |
-
|
| 44 |
-
class ToolCall(BaseModel):
|
| 45 |
-
id: str
|
| 46 |
-
function: dict
|
| 47 |
-
|
| 48 |
-
# Simple in-memory models storage
|
| 49 |
-
class ModelInfo:
|
| 50 |
-
def __init__(self, name, status, description):
|
| 51 |
-
self.name = name
|
| 52 |
-
self.status = status
|
| 53 |
-
self.description = description
|
| 54 |
|
| 55 |
@app.get("/")
|
| 56 |
async def root():
|
| 57 |
return {
|
| 58 |
-
"message": "AI Model Runner
|
| 59 |
"version": "1.0.0",
|
| 60 |
"status": "running",
|
| 61 |
-
"endpoints":
|
| 62 |
-
"chat": "/chat",
|
| 63 |
-
"code": "/code",
|
| 64 |
-
"reasoning": "/reasoning",
|
| 65 |
-
"sentiment": "/analyze-sentiment",
|
| 66 |
-
"agentic": "/agentic",
|
| 67 |
-
"upload": "/upload",
|
| 68 |
-
"models": "/models"
|
| 69 |
-
}
|
| 70 |
}
|
| 71 |
|
| 72 |
@app.get("/health")
|
|
@@ -75,257 +33,76 @@ async def health_check():
|
|
| 75 |
|
| 76 |
@app.get("/models")
|
| 77 |
async def get_models():
|
| 78 |
-
models = [
|
| 79 |
-
ModelInfo("lightweight-chat", "loaded", "Lightweight conversational model"),
|
| 80 |
-
ModelInfo("simple-code", "loaded", "Basic code analysis model"),
|
| 81 |
-
ModelInfo("text-reasoner", "loaded", "Problem solving assistant"),
|
| 82 |
-
ModelInfo("sentiment-analyzer", "loaded", "Text sentiment analysis"),
|
| 83 |
-
ModelInfo("agentic-model", "loaded", "Agentic model with exceptional tool use and Interleaved Thinking")
|
| 84 |
-
]
|
| 85 |
return {
|
| 86 |
"models": [
|
| 87 |
{
|
| 88 |
-
"name": model
|
| 89 |
-
"status":
|
| 90 |
-
"description": model
|
| 91 |
-
}
|
| 92 |
]
|
| 93 |
}
|
| 94 |
|
| 95 |
-
@app.post("/chat")
|
| 96 |
-
async def chat_endpoint(request: ChatRequest):
|
| 97 |
-
try:
|
| 98 |
-
# Simulated lightweight response
|
| 99 |
-
if "hello" in request.message.lower():
|
| 100 |
-
response = "Hello! I'm your AI assistant. How can I help you today?"
|
| 101 |
-
elif "code" in request.message.lower():
|
| 102 |
-
response = "I can help analyze code! Please provide the code you'd like me to review."
|
| 103 |
-
elif "reason" in request.message.lower():
|
| 104 |
-
response = "I'm here to help with problem-solving and reasoning. What's the challenge you're facing?"
|
| 105 |
-
else:
|
| 106 |
-
response = f"You said: '{request.message}'. This is a response from the lightweight AI model."
|
| 107 |
-
|
| 108 |
-
return {
|
| 109 |
-
"response": response,
|
| 110 |
-
"conversation_id": f"conv_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 111 |
-
"status": "success"
|
| 112 |
-
}
|
| 113 |
-
except Exception as e:
|
| 114 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 115 |
-
|
| 116 |
-
@app.post("/code")
|
| 117 |
-
async def code_endpoint(request: CodeRequest):
|
| 118 |
-
try:
|
| 119 |
-
# Simulated lightweight code analysis
|
| 120 |
-
if len(request.code.strip()) == 0:
|
| 121 |
-
return {"error": "No code provided"}
|
| 122 |
-
|
| 123 |
-
analysis = {
|
| 124 |
-
"language": "Unknown",
|
| 125 |
-
"complexity": "Low",
|
| 126 |
-
"issues": [],
|
| 127 |
-
"suggestions": [],
|
| 128 |
-
"explanation": f"Code analysis for: {request.code[:100]}..."
|
| 129 |
-
}
|
| 130 |
-
|
| 131 |
-
if "def " in request.code:
|
| 132 |
-
analysis["language"] = "Python"
|
| 133 |
-
analysis["explanation"] = "This appears to be a Python function definition"
|
| 134 |
-
elif "function" in request.code or "function(" in request.code:
|
| 135 |
-
analysis["language"] = "JavaScript"
|
| 136 |
-
analysis["explanation"] = "This appears to be a JavaScript function"
|
| 137 |
-
|
| 138 |
-
return analysis
|
| 139 |
-
except Exception as e:
|
| 140 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 141 |
-
|
| 142 |
-
@app.post("/reasoning")
|
| 143 |
-
async def reasoning_endpoint(request: ReasoningRequest):
|
| 144 |
-
try:
|
| 145 |
-
# Simulated reasoning response
|
| 146 |
-
problem = request.problem
|
| 147 |
-
if "math" in problem.lower() or any(char.isdigit() for char in problem):
|
| 148 |
-
response = "This appears to be a mathematical problem. Let me break it down step by step."
|
| 149 |
-
elif "logic" in problem.lower():
|
| 150 |
-
response = "This is a logical reasoning problem. Let's analyze the given information and draw conclusions."
|
| 151 |
-
else:
|
| 152 |
-
response = f"Analyzing your problem: '{problem}'. This requires careful step-by-step reasoning."
|
| 153 |
-
|
| 154 |
-
return {
|
| 155 |
-
"reasoning": response,
|
| 156 |
-
"steps": [
|
| 157 |
-
"Understand the problem",
|
| 158 |
-
"Identify key information",
|
| 159 |
-
"Apply logical reasoning",
|
| 160 |
-
"Draw conclusions"
|
| 161 |
-
],
|
| 162 |
-
"confidence": 0.85
|
| 163 |
-
}
|
| 164 |
-
except Exception as e:
|
| 165 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 166 |
-
|
| 167 |
-
@app.post("/analyze-sentiment")
|
| 168 |
-
async def sentiment_endpoint(request: SentimentRequest):
|
| 169 |
-
try:
|
| 170 |
-
text = request.text.lower()
|
| 171 |
-
|
| 172 |
-
# Simple sentiment analysis
|
| 173 |
-
positive_words = ["good", "great", "excellent", "amazing", "wonderful", "fantastic", "love", "like"]
|
| 174 |
-
negative_words = ["bad", "terrible", "awful", "horrible", "hate", "dislike", "worst", "disaster"]
|
| 175 |
-
|
| 176 |
-
pos_score = sum(1 for word in positive_words if word in text)
|
| 177 |
-
neg_score = sum(1 for word in negative_words if word in text)
|
| 178 |
-
|
| 179 |
-
if pos_score > neg_score:
|
| 180 |
-
sentiment = "positive"
|
| 181 |
-
confidence = 0.8
|
| 182 |
-
elif neg_score > pos_score:
|
| 183 |
-
sentiment = "negative"
|
| 184 |
-
confidence = 0.8
|
| 185 |
-
else:
|
| 186 |
-
sentiment = "neutral"
|
| 187 |
-
confidence = 0.6
|
| 188 |
-
|
| 189 |
-
return {
|
| 190 |
-
"sentiment": sentiment,
|
| 191 |
-
"confidence": confidence,
|
| 192 |
-
"text": request.text
|
| 193 |
-
}
|
| 194 |
-
except Exception as e:
|
| 195 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 196 |
-
|
| 197 |
-
@app.post("/upload")
|
| 198 |
-
async def upload_endpoint():
|
| 199 |
-
return {
|
| 200 |
-
"message": "File upload endpoint ready",
|
| 201 |
-
"supported_formats": ["txt", "py", "js", "html", "md"],
|
| 202 |
-
"max_size": "10MB"
|
| 203 |
-
}
|
| 204 |
-
|
| 205 |
@app.post("/agentic")
|
| 206 |
async def agentic_endpoint(request: AgenticRequest):
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
"name": "search_web",
|
| 212 |
-
"
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
{
|
| 222 |
"name": "analyze_code",
|
| 223 |
-
"
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
{
|
| 234 |
"name": "get_weather",
|
| 235 |
-
"
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
{
|
| 245 |
"name": "math_calc",
|
| 246 |
-
"
|
| 247 |
-
"parameters": {
|
| 248 |
-
"type": "object",
|
| 249 |
-
"properties": {
|
| 250 |
-
"expression": {"type": "string", "description": "Mathematical expression to evaluate"}
|
| 251 |
-
},
|
| 252 |
-
"required": ["expression"]
|
| 253 |
-
}
|
| 254 |
}
|
| 255 |
-
]
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
"function": {
|
| 269 |
-
"name": "search_web",
|
| 270 |
-
"arguments": f'{{"query": "{request.task}"}}'
|
| 271 |
-
}
|
| 272 |
-
}]
|
| 273 |
-
content = f"I've analyzed your task: '{request.task}'. I'm searching for relevant information to provide a comprehensive response."
|
| 274 |
-
|
| 275 |
-
elif "code" in task_lower or "analyze" in task_lower:
|
| 276 |
-
reasoning_details = "This task requires code analysis. I need to analyze the code structure and identify potential improvements. Let me use the analyze_code tool."
|
| 277 |
-
tool_calls = [{
|
| 278 |
-
"id": f"call_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 279 |
-
"function": {
|
| 280 |
-
"name": "analyze_code",
|
| 281 |
-
"arguments": f'{{"code": "{request.context}", "task": "comprehensive_analysis"}}'
|
| 282 |
-
}
|
| 283 |
-
}]
|
| 284 |
-
content = f"Analyzing your code-related task: '{request.task}'. I'm performing a detailed code analysis to provide insights and recommendations."
|
| 285 |
-
|
| 286 |
-
elif "weather" in task_lower or "temperature" in task_lower:
|
| 287 |
-
reasoning_details = "This task requires current weather information. I need to get the weather data for the specified location using the get_weather tool."
|
| 288 |
-
# Extract location from task or context
|
| 289 |
-
location = "current location" # default
|
| 290 |
-
if request.context:
|
| 291 |
-
location = request.context
|
| 292 |
-
tool_calls = [{
|
| 293 |
-
"id": f"call_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 294 |
-
"function": {
|
| 295 |
-
"name": "get_weather",
|
| 296 |
-
"arguments": f'{{"location": "{location}"}}'
|
| 297 |
-
}
|
| 298 |
-
}]
|
| 299 |
-
content = f"Getting weather information for your request: '{request.task}'."
|
| 300 |
-
|
| 301 |
-
elif any(char.isdigit() for char in task_lower) and ("calculate" in task_lower or "compute" in task_lower or "+" in task_lower or "-" in task_lower or "*" in task_lower or "/" in task_lower):
|
| 302 |
-
reasoning_details = "This is a mathematical computation task. I need to evaluate the mathematical expression to provide an accurate result."
|
| 303 |
-
tool_calls = [{
|
| 304 |
-
"id": f"call_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 305 |
-
"function": {
|
| 306 |
-
"name": "math_calc",
|
| 307 |
-
"arguments": f'{{"expression": "{request.task}"}}'
|
| 308 |
-
}
|
| 309 |
-
}]
|
| 310 |
-
content = f"Processing your mathematical task: '{request.task}'."
|
| 311 |
-
|
| 312 |
-
else:
|
| 313 |
-
# General reasoning task
|
| 314 |
-
reasoning_details = "This is a complex reasoning task that requires analysis and step-by-step thinking. Let me break this down systematically."
|
| 315 |
-
content = f"Working on your task: '{request.task}'. I'm applying systematic reasoning to provide a comprehensive solution."
|
| 316 |
-
# In a real implementation, this might trigger multiple tool calls
|
| 317 |
-
|
| 318 |
-
return {
|
| 319 |
-
"reasoning_details": reasoning_details,
|
| 320 |
-
"content": content,
|
| 321 |
-
"tool_calls": tool_calls,
|
| 322 |
-
"available_tools": available_tools,
|
| 323 |
-
"status": "success",
|
| 324 |
-
"thinking_process": "Interleaved thinking enabled - reasoning performed between tool interactions"
|
| 325 |
-
}
|
| 326 |
-
|
| 327 |
-
except Exception as e:
|
| 328 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 329 |
|
| 330 |
if __name__ == "__main__":
|
| 331 |
import uvicorn
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
|
|
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
+
app = FastAPI(title="AI Model Runner - Simple", version="1.0.0")
|
| 7 |
|
| 8 |
# Enable CORS
|
| 9 |
app.add_middleware(
|
|
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
class AgenticRequest(BaseModel):
|
| 18 |
task: str
|
| 19 |
context: str = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.get("/")
|
| 22 |
async def root():
|
| 23 |
return {
|
| 24 |
+
"message": "AI Model Runner - Simple",
|
| 25 |
"version": "1.0.0",
|
| 26 |
"status": "running",
|
| 27 |
+
"endpoints": ["/", "/health", "/models", "/agentic"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
| 29 |
|
| 30 |
@app.get("/health")
|
|
|
|
| 33 |
|
| 34 |
@app.get("/models")
|
| 35 |
async def get_models():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
return {
|
| 37 |
"models": [
|
| 38 |
{
|
| 39 |
+
"name": "agentic-model",
|
| 40 |
+
"status": "loaded",
|
| 41 |
+
"description": "Agentic model with exceptional tool use and Interleaved Thinking"
|
| 42 |
+
}
|
| 43 |
]
|
| 44 |
}
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
@app.post("/agentic")
|
| 47 |
async def agentic_endpoint(request: AgenticRequest):
|
| 48 |
+
# Simulate Interleaved Thinking process
|
| 49 |
+
task_lower = request.task.lower()
|
| 50 |
+
reasoning_details = ""
|
| 51 |
+
tool_calls = []
|
| 52 |
+
content = ""
|
| 53 |
+
|
| 54 |
+
if "search" in task_lower or "find" in task_lower:
|
| 55 |
+
reasoning_details = "I need to search for information to complete this task. Let me use the search_web tool."
|
| 56 |
+
tool_calls = [{
|
| 57 |
+
"id": f"call_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 58 |
+
"function": {
|
| 59 |
"name": "search_web",
|
| 60 |
+
"arguments": f'{{"query": "{request.task}"}}'
|
| 61 |
+
}
|
| 62 |
+
}]
|
| 63 |
+
content = f"I've analyzed your task: '{request.task}'. I'm searching for relevant information."
|
| 64 |
+
elif "code" in task_lower:
|
| 65 |
+
reasoning_details = "This task requires code analysis. I need to analyze the code structure."
|
| 66 |
+
tool_calls = [{
|
| 67 |
+
"id": f"call_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 68 |
+
"function": {
|
|
|
|
| 69 |
"name": "analyze_code",
|
| 70 |
+
"arguments": f'{{"code": "{request.context}", "task": "comprehensive_analysis"}}'
|
| 71 |
+
}
|
| 72 |
+
}]
|
| 73 |
+
content = f"Analyzing your code-related task: '{request.task}'."
|
| 74 |
+
elif "weather" in task_lower:
|
| 75 |
+
location = request.context or "current location"
|
| 76 |
+
reasoning_details = "This task requires weather information. I need to get current weather data."
|
| 77 |
+
tool_calls = [{
|
| 78 |
+
"id": f"call_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 79 |
+
"function": {
|
|
|
|
| 80 |
"name": "get_weather",
|
| 81 |
+
"arguments": f'{{"location": "{location}"}}'
|
| 82 |
+
}
|
| 83 |
+
}]
|
| 84 |
+
content = f"Getting weather information for: '{request.task}'."
|
| 85 |
+
elif any(char.isdigit() for char in task_lower) and ("+" in task_lower or "-" in task_lower or "*" in task_lower):
|
| 86 |
+
reasoning_details = "This is a mathematical computation task. I need to evaluate the expression."
|
| 87 |
+
tool_calls = [{
|
| 88 |
+
"id": f"call_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 89 |
+
"function": {
|
|
|
|
| 90 |
"name": "math_calc",
|
| 91 |
+
"arguments": f'{{"expression": "{request.task}"}}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
}
|
| 93 |
+
}]
|
| 94 |
+
content = f"Processing mathematical task: '{request.task}'."
|
| 95 |
+
else:
|
| 96 |
+
reasoning_details = "This is a complex reasoning task requiring systematic analysis."
|
| 97 |
+
content = f"Working on your task: '{request.task}'. Applying systematic reasoning."
|
| 98 |
+
|
| 99 |
+
return {
|
| 100 |
+
"reasoning_details": reasoning_details,
|
| 101 |
+
"content": content,
|
| 102 |
+
"tool_calls": tool_calls,
|
| 103 |
+
"status": "success",
|
| 104 |
+
"thinking_process": "Interleaved thinking enabled - reasoning performed between tool interactions"
|
| 105 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
if __name__ == "__main__":
|
| 108 |
import uvicorn
|