Austinsz-Warehouse commited on
Commit
9301515
·
verified ·
1 Parent(s): b6cf9ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -17
app.py CHANGED
@@ -1,7 +1,19 @@
1
  import gradio as gr
 
 
2
 
3
  # -------------------------
4
- # In-memory character storage (demo)
 
 
 
 
 
 
 
 
 
 
5
  # -------------------------
6
  characters = [
7
  {
@@ -16,15 +28,25 @@ characters = [
16
  # -------------------------
17
  def chat_with_character(message, chat_history, system_prompt):
18
  """
19
- Simple chatbot using system_prompt and history.
20
- In a real app, replace with API call to pretrained model.
21
  """
22
- # Prefix system message
23
- prompt = f"{system_prompt}\nUser: {message}\nAI:"
24
-
25
- # For demo purposes, just echo input + system prompt
26
- response = f"[{system_prompt.split('.')[0]} Response]: {message[::-1]}" # Reverse text for demo
27
-
 
 
 
 
 
 
 
 
 
 
 
28
  chat_history.append((message, response))
29
  return chat_history
30
 
@@ -50,31 +72,30 @@ def search_characters(query):
50
  # Build Gradio GUI
51
  # -------------------------
52
  with gr.Blocks(title="AI Character Hub") as demo:
53
-
54
  gr.Markdown("## Welcome to the AI Character Hub 🧠\nCreate, explore, and chat with AI characters!")
55
-
56
- # Sidebar: character search
57
  with gr.Sidebar():
58
  gr.Markdown("### Explore Characters")
59
  search_input = gr.Textbox(label="Search by name")
60
  search_btn = gr.Button("Search")
61
  search_output = gr.Textbox(label="Results")
62
  search_btn.click(search_characters, inputs=search_input, outputs=search_output)
63
-
64
- # Main area: Tabs
65
  with gr.Tabs():
66
  with gr.Tab("Premade Tutorial"):
67
  gr.Markdown("### Chat with the premade tutorial bot")
68
  tutorial_chatbot = gr.Chatbot(label="TutorialBot")
69
  tutorial_input = gr.Textbox(label="Your message")
70
  tutorial_send = gr.Button("Send")
71
-
72
  def tutorial_send_message(msg, history):
73
  return chat_with_character(msg, history, characters[0]["system_prompt"])
74
-
75
  tutorial_send.click(tutorial_send_message, inputs=[tutorial_input, tutorial_chatbot], outputs=tutorial_chatbot)
76
  tutorial_input.submit(tutorial_send_message, inputs=[tutorial_input, tutorial_chatbot], outputs=tutorial_chatbot)
77
-
78
  with gr.Tab("Create Your Character"):
79
  gr.Markdown("### Create a new AI character")
80
  char_name = gr.Textbox(label="Character Name")
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
 
5
  # -------------------------
6
+ # Load a pretrained model for all chatbots
7
+ # -------------------------
8
+ MODEL_NAME = "EleutherAI/gpt-neo-1.3B" # CPU friendly smaller model
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
11
+
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+ model.to(device)
14
+
15
+ # -------------------------
16
+ # In-memory character storage
17
  # -------------------------
18
  characters = [
19
  {
 
28
  # -------------------------
29
  def chat_with_character(message, chat_history, system_prompt):
30
  """
31
+ Generates real AI responses using the pretrained model.
 
32
  """
33
+ prompt_text = system_prompt + "\n"
34
+ for user_msg, ai_msg in chat_history:
35
+ prompt_text += f"User: {user_msg}\nAI: {ai_msg}\n"
36
+ prompt_text += f"User: {message}\nAI:"
37
+
38
+ inputs = tokenizer(prompt_text, return_tensors="pt").to(device)
39
+ outputs = model.generate(
40
+ **inputs,
41
+ max_new_tokens=150,
42
+ pad_token_id=tokenizer.eos_token_id,
43
+ do_sample=True,
44
+ temperature=0.7,
45
+ top_p=0.9
46
+ )
47
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
48
+ response = response[len(prompt_text):].strip()
49
+
50
  chat_history.append((message, response))
51
  return chat_history
52
 
 
72
  # Build Gradio GUI
73
  # -------------------------
74
  with gr.Blocks(title="AI Character Hub") as demo:
 
75
  gr.Markdown("## Welcome to the AI Character Hub 🧠\nCreate, explore, and chat with AI characters!")
76
+
77
+ # Sidebar: search
78
  with gr.Sidebar():
79
  gr.Markdown("### Explore Characters")
80
  search_input = gr.Textbox(label="Search by name")
81
  search_btn = gr.Button("Search")
82
  search_output = gr.Textbox(label="Results")
83
  search_btn.click(search_characters, inputs=search_input, outputs=search_output)
84
+
85
+ # Tabs for premade + create
86
  with gr.Tabs():
87
  with gr.Tab("Premade Tutorial"):
88
  gr.Markdown("### Chat with the premade tutorial bot")
89
  tutorial_chatbot = gr.Chatbot(label="TutorialBot")
90
  tutorial_input = gr.Textbox(label="Your message")
91
  tutorial_send = gr.Button("Send")
92
+
93
  def tutorial_send_message(msg, history):
94
  return chat_with_character(msg, history, characters[0]["system_prompt"])
95
+
96
  tutorial_send.click(tutorial_send_message, inputs=[tutorial_input, tutorial_chatbot], outputs=tutorial_chatbot)
97
  tutorial_input.submit(tutorial_send_message, inputs=[tutorial_input, tutorial_chatbot], outputs=tutorial_chatbot)
98
+
99
  with gr.Tab("Create Your Character"):
100
  gr.Markdown("### Create a new AI character")
101
  char_name = gr.Textbox(label="Character Name")