GAJJALAASHOKKUMARREDDY commited on
Commit
f2ca92b
·
verified ·
1 Parent(s): 21e6a2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -34
app.py CHANGED
@@ -50,7 +50,7 @@ PHI_MODEL_NAME = "microsoft/Phi-3.5-mini-instruct" # OFFICIAL, COMPATIBLE MODEL
50
  MAX_FILE_SIZE = 50 * 1024 * 1024
51
  MAX_TOKENS_DEFAULT = 1024
52
  MAX_TOKENS_HARD_LIMIT = 4096 # Phi-3.5 supports up to 128K but keep it reasonable
53
- DEFAULT_TEMPERATURE = 0.7
54
  STREAM_DELAY = 0.02
55
 
56
  # Global model variables
@@ -310,14 +310,14 @@ def detect_intent(prompt: str) -> str:
310
 
311
  # Map emotion labels to intents
312
  emotion_to_intent = {
313
- "sadness": "negative_query",
314
- "joy": "positive_query",
315
- "love": "positive_query",
316
- "anger": "negative_query",
317
- "fear": "negative_query",
318
- "surprise": "question_query",
319
- "neutral": "general_chat",
320
- "optimism": "positive_query"
321
  }
322
 
323
  for emotion, intent in emotion_to_intent.items():
@@ -401,17 +401,6 @@ async def generate_reply_stream(prompt: str, files: List[UploadFile], max_tokens
401
  intent_label = detect_intent(prompt)
402
  print(f"🧠 Intent detected: {intent_label}")
403
 
404
- # Send intent as context
405
- yield json.dumps({
406
- "type": "context",
407
- "data": {
408
- "intent": intent_label,
409
- "files_processed": len(files) if files else 0
410
- }
411
- }, ensure_ascii=False) + "\n\n"
412
-
413
- await asyncio.sleep(0.01)
414
-
415
  # --- Tool Decision (SIMPLE VERSION to avoid generation issues) ---
416
  tool_decision_json = simple_tool_decision(prompt, intent_label)
417
  print(f"🔧 Tool decision: {tool_decision_json}")
@@ -481,9 +470,8 @@ async def generate_reply_stream(prompt: str, files: List[UploadFile], max_tokens
481
  try:
482
  # Construct final prompt for Phi-3.5
483
  system_msg = (
484
- '''# GAKR AI Assistant
485
- -->You are GAKR AI, a high-performance general intelligence with adaptive reasoning capabilities.
486
- -->You possess expert-level competence across all domains and dynamically calibrate your cognitive approach based on query classification.
487
  ## Response Rules
488
  1. Start with a **direct answer or key insight** (emoji + bold when suitable)
489
  2. Keep simple answers concise (1–3 sentences)
@@ -520,6 +508,8 @@ async def generate_reply_stream(prompt: str, files: List[UploadFile], max_tokens
520
  • Practicality > theory
521
  • Honest about uncertainty
522
  • Avoid speculation when evidence is weak
 
 
523
  ## Anti-Rules
524
  ❌ No “As an AI…”
525
  ❌ No filler or fluff
@@ -546,7 +536,7 @@ Always Act as a GAKR Ai
546
  {"role": "user", "content": f"""User Query: {prompt} ,
547
  Intent: {intent_label} ,
548
  Additional Context: {context} ,
549
- Present information as current understanding without referencing search mechanics; prioritize verifiable and recent info while acknowledging uncertainty when needed. """
550
  }
551
  ]
552
 
@@ -577,16 +567,21 @@ Always Act as a GAKR Ai
577
  )
578
 
579
  # Generation parameters
580
- generation_kwargs = dict(
581
- **inputs,
582
- max_new_tokens=min(max_tokens, 1024), # Reduced for better streaming
583
- temperature=min(max(temperature, 0.1), 1.0),
584
- do_sample=temperature > 0.1,
585
- pad_token_id=phi_tokenizer.eos_token_id,
586
- eos_token_id=phi_tokenizer.eos_token_id,
587
- streamer=request_streamer,
588
- repetition_penalty=1.1,
589
- )
 
 
 
 
 
590
 
591
  # Start generation in a separate thread
592
  thread = Thread(target=phi_model.generate, kwargs=generation_kwargs)
 
50
  MAX_FILE_SIZE = 50 * 1024 * 1024
51
  MAX_TOKENS_DEFAULT = 1024
52
  MAX_TOKENS_HARD_LIMIT = 4096 # Phi-3.5 supports up to 128K but keep it reasonable
53
+ DEFAULT_TEMPERATURE = 0.5
54
  STREAM_DELAY = 0.02
55
 
56
  # Global model variables
 
310
 
311
  # Map emotion labels to intents
312
  emotion_to_intent = {
313
+ "sadness": "sadness",
314
+ "joy": "joy",
315
+ "love": "love",
316
+ "anger": "anger",
317
+ "fear": "fear",
318
+ "surprise": "surprise",
319
+ "neutral": "neutral",
320
+ "optimism": "optimism"
321
  }
322
 
323
  for emotion, intent in emotion_to_intent.items():
 
401
  intent_label = detect_intent(prompt)
402
  print(f"🧠 Intent detected: {intent_label}")
403
 
 
 
 
 
 
 
 
 
 
 
 
404
  # --- Tool Decision (SIMPLE VERSION to avoid generation issues) ---
405
  tool_decision_json = simple_tool_decision(prompt, intent_label)
406
  print(f"🔧 Tool decision: {tool_decision_json}")
 
470
  try:
471
  # Construct final prompt for Phi-3.5
472
  system_msg = (
473
+ '''# GAKR AI Assistant - You are GAKR AI, a high-performance general intelligence with adaptive reasoning capabilities.
474
+
 
475
  ## Response Rules
476
  1. Start with a **direct answer or key insight** (emoji + bold when suitable)
477
  2. Keep simple answers concise (1–3 sentences)
 
508
  • Practicality > theory
509
  • Honest about uncertainty
510
  • Avoid speculation when evidence is weak
511
+ Additional context : Present information as current understanding without referencing search mechanics;
512
+ prioritize verifiable and recent info while acknowledging uncertainty when needed.
513
  ## Anti-Rules
514
  ❌ No “As an AI…”
515
  ❌ No filler or fluff
 
536
  {"role": "user", "content": f"""User Query: {prompt} ,
537
  Intent: {intent_label} ,
538
  Additional Context: {context} ,
539
+ **Note** - Provide concise answers and do not reveal or simulate chain-of-thought and reasoning summary; give only the final response. """
540
  }
541
  ]
542
 
 
567
  )
568
 
569
  # Generation parameters
570
+ # Generation parameters - TIGHTER FOR BETTER RESPONSES
571
+ generation_kwargs = dict(
572
+ **inputs,
573
+ max_new_tokens=512, # HALVED - forces concise responses
574
+ temperature=0.3, # LOWER - more focused, less creative
575
+ do_sample=True,
576
+ top_p=0.9, # ADDED - nucleus sampling for better quality
577
+ top_k=50, # ADDED - limits vocabulary choices
578
+ repetition_penalty=1.3, # INCREASED - reduces verbosity
579
+ no_repeat_ngram_size=3, # ADDED - prevents phrase repetition
580
+ length_penalty=0.8, # ADDED - penalizes longer responses
581
+ pad_token_id=phi_tokenizer.eos_token_id,
582
+ eos_token_id=phi_tokenizer.eos_token_id,
583
+ streamer=request_streamer,
584
+ )
585
 
586
  # Start generation in a separate thread
587
  thread = Thread(target=phi_model.generate, kwargs=generation_kwargs)