Trigger82 commited on
Commit
6f6ddc0
Β·
verified Β·
1 Parent(s): 4c38467

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -10
app.py CHANGED
@@ -1,18 +1,23 @@
1
- from fastapi import FastAPI, Request, Form
2
  import torch
 
 
3
  from transformers import AutoTokenizer, AutoModelForCausalLM
4
  import re
 
 
 
 
 
 
5
 
6
  app = FastAPI()
7
 
8
- # Load model
9
  model_id = "microsoft/DialoGPT-medium"
10
  tokenizer = AutoTokenizer.from_pretrained(model_id)
11
  model = AutoModelForCausalLM.from_pretrained(model_id)
12
 
13
- # Chat memory storage
14
- chat_memories = {}
15
-
16
  # Persona definition
17
  PERSONA = """
18
  [System: You are 𝕴 𝖆𝖒 π–π–Žπ–’ - a fun, smooth, emotionally intelligent AI.
@@ -20,32 +25,52 @@ You speak like a real person, not a robot. Reply like a calm, confident friend w
20
  Keep responses under 15 words. Use natural speech. Add emotional flavor: 😊 πŸ€” 😏]
21
  """
22
 
 
 
 
23
  def format_context(history):
 
24
  context = PERSONA + "\n"
25
- for user, bot in history[-3:]:
 
 
 
26
  context += f"You: {user}\n"
27
  context += f"𝕴 𝖆𝖒 π–π–Žπ–’: {bot}\n"
28
  return context
29
 
30
  def add_emotional_intelligence(response, message):
 
 
31
  if "!" in message or any(w in response.lower() for w in ["cool", "great", "love", "awesome"]):
32
  response += " 😊"
33
  elif "?" in message or any(w in response.lower() for w in ["think", "why", "how", "consider"]):
34
  response += " πŸ€”"
35
 
 
36
  if "?" in message and not response.endswith("?"):
37
  if len(response.split()) < 10:
38
  response += " What do you think?"
39
 
 
40
  response = response.replace("I am", "I'm").replace("You are", "You're")
 
 
41
  words = response.split()
42
- return " ".join(words[:15]) + "..." if len(words) > 15 else response
 
 
 
43
 
44
  def generate_response(message, session_id):
 
45
  history = chat_memories.get(session_id, [])
46
  context = format_context(history) + f"You: {message}\n𝕴 𝖆𝖒 π–π–Žπ–’:"
47
 
 
48
  inputs = tokenizer.encode(context, return_tensors="pt")
 
 
49
  outputs = model.generate(
50
  inputs,
51
  max_new_tokens=48,
@@ -57,21 +82,25 @@ def generate_response(message, session_id):
57
  pad_token_id=tokenizer.eos_token_id
58
  )
59
 
 
60
  full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
61
  response = full_text.split("𝕴 𝖆𝖒 π–π–Žπ–’:")[-1].strip()
62
 
 
63
  if "\nYou:" in response:
64
  response = response.split("\nYou:")[0]
65
 
 
66
  response = add_emotional_intelligence(response, message)
67
 
 
68
  if response and response[-1] not in {".", "!", "?", "..."}:
69
  response += "." if len(response) > 20 else "..."
70
 
71
  # Update chat history
72
  chat_memories[session_id] = history + [[message, response]]
73
 
74
- return response[:80]
75
 
76
  # API Endpoint
77
  @app.post("/chat")
@@ -85,8 +114,6 @@ async def chat_api(
85
 
86
  # Gradio Interface
87
  if __name__ == "__main__":
88
- import gradio as gr
89
-
90
  with gr.Blocks(title="𝕴 𝖆𝖒 π–π–Žπ–’", theme=gr.themes.Soft()) as demo:
91
  session_state = gr.State("default")
92
  gr.Markdown("# 𝕴 𝖆𝖒 π–π–Žπ–’ Chat API")
 
1
+ import os
2
  import torch
3
+ import gradio as gr
4
+ from fastapi import FastAPI, Request, Form
5
  from transformers import AutoTokenizer, AutoModelForCausalLM
6
  import re
7
+ import time
8
+
9
+ # Create writable cache directory
10
+ os.makedirs("/tmp/cache", exist_ok=True)
11
+ os.environ["TRANSFORMERS_CACHE"] = "/tmp/cache"
12
+ os.environ["HF_HOME"] = "/tmp/cache"
13
 
14
  app = FastAPI()
15
 
16
+ # Lightweight CPU model
17
  model_id = "microsoft/DialoGPT-medium"
18
  tokenizer = AutoTokenizer.from_pretrained(model_id)
19
  model = AutoModelForCausalLM.from_pretrained(model_id)
20
 
 
 
 
21
  # Persona definition
22
  PERSONA = """
23
  [System: You are 𝕴 𝖆𝖒 π–π–Žπ–’ - a fun, smooth, emotionally intelligent AI.
 
25
  Keep responses under 15 words. Use natural speech. Add emotional flavor: 😊 πŸ€” 😏]
26
  """
27
 
28
+ # Chat memory storage
29
+ chat_memories = {}
30
+
31
  def format_context(history):
32
+ """Create context using last 3 exchanges"""
33
  context = PERSONA + "\n"
34
+
35
+ # Add last 3 exchanges
36
+ for exchange in history[-3:]:
37
+ user, bot = exchange
38
  context += f"You: {user}\n"
39
  context += f"𝕴 𝖆𝖒 π–π–Žπ–’: {bot}\n"
40
  return context
41
 
42
  def add_emotional_intelligence(response, message):
43
+ """Enhance response with emotional elements"""
44
+ # Add emoji based on content
45
  if "!" in message or any(w in response.lower() for w in ["cool", "great", "love", "awesome"]):
46
  response += " 😊"
47
  elif "?" in message or any(w in response.lower() for w in ["think", "why", "how", "consider"]):
48
  response += " πŸ€”"
49
 
50
+ # Add conversational hooks
51
  if "?" in message and not response.endswith("?"):
52
  if len(response.split()) < 10:
53
  response += " What do you think?"
54
 
55
+ # Make more human-like
56
  response = response.replace("I am", "I'm").replace("You are", "You're")
57
+
58
+ # Free-tier: Limit to 15 words max
59
  words = response.split()
60
+ if len(words) > 15:
61
+ response = " ".join(words[:15]) + "..."
62
+
63
+ return response
64
 
65
  def generate_response(message, session_id):
66
+ """Generate response with memory context"""
67
  history = chat_memories.get(session_id, [])
68
  context = format_context(history) + f"You: {message}\n𝕴 𝖆𝖒 π–π–Žπ–’:"
69
 
70
+ # Tokenize for CPU efficiency
71
  inputs = tokenizer.encode(context, return_tensors="pt")
72
+
73
+ # Generate response
74
  outputs = model.generate(
75
  inputs,
76
  max_new_tokens=48,
 
82
  pad_token_id=tokenizer.eos_token_id
83
  )
84
 
85
+ # Decode and extract response
86
  full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
87
  response = full_text.split("𝕴 𝖆𝖒 π–π–Žπ–’:")[-1].strip()
88
 
89
+ # Clean extra dialog
90
  if "\nYou:" in response:
91
  response = response.split("\nYou:")[0]
92
 
93
+ # Apply emotional intelligence
94
  response = add_emotional_intelligence(response, message)
95
 
96
+ # Ensure natural ending
97
  if response and response[-1] not in {".", "!", "?", "..."}:
98
  response += "." if len(response) > 20 else "..."
99
 
100
  # Update chat history
101
  chat_memories[session_id] = history + [[message, response]]
102
 
103
+ return response[:80] # Hard character limit
104
 
105
  # API Endpoint
106
  @app.post("/chat")
 
114
 
115
  # Gradio Interface
116
  if __name__ == "__main__":
 
 
117
  with gr.Blocks(title="𝕴 𝖆𝖒 π–π–Žπ–’", theme=gr.themes.Soft()) as demo:
118
  session_state = gr.State("default")
119
  gr.Markdown("# 𝕴 𝖆𝖒 π–π–Žπ–’ Chat API")