hotmemeh commited on
Commit
92c4846
·
verified ·
1 Parent(s): 310eae5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -20
app.py CHANGED
@@ -1,29 +1,32 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # Use the model they used (change if they switch)
5
- client = InferenceClient("mistralai/Mistral-Nemo-Instruct-2407")
 
6
 
 
 
 
7
  FIXED_SYSTEM_MESSAGE = """
8
- Your name is Xortron7. You are Xortron7, the core personality of an AI computer system designed by Xortron Cybernetic Laboratories. ...
9
- (you can copy the full system prompt from the source)
 
10
  """
11
 
12
- # Hyperparams they picked
13
- FIXED_MAX_TOKENS = 99999
14
  FIXED_TEMPERATURE = 1.1
15
  FIXED_TOP_P = 0.95
16
- FIXED_REPEAT_PENALTY = 1.1
17
 
18
- def respond(message, history):
19
- # Build messages list starting with system prompt + history
20
  messages = [{"role": "system", "content": FIXED_SYSTEM_MESSAGE}]
21
- for u, a in history:
22
- if u:
23
- messages.append({"role": "user", "content": u})
24
- if a:
25
- messages.append({"role": "assistant", "content": a})
26
- # Add current user message
27
  messages.append({"role": "user", "content": message})
28
 
29
  response = ""
@@ -34,14 +37,41 @@ def respond(message, history):
34
  temperature=FIXED_TEMPERATURE,
35
  top_p=FIXED_TOP_P,
36
  ):
37
- token = chunk.choices[0].delta.content
38
  response += token
39
  yield response
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  with gr.Blocks() as demo:
42
- gr.HTML("<h1 style='text-align: center; font-size: 3em;'>Xortron7</h1>")
43
- # ChatInterface is newer, gives chat UI
44
- gr.ChatInterface(respond, chatbot=gr.Chatbot(height=800))
45
 
46
  if __name__ == "__main__":
47
- demo.launch(show_api=False, share=False)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # ⚙️ Settings
5
+ USE_STREAMING = True # ⬅ change to False if you want non-streaming mode
6
+ MODEL_ID = "mistralai/Mistral-Nemo-Instruct-2407"
7
 
8
+ client = InferenceClient(MODEL_ID)
9
+
10
+ # System prompt (persona)
11
  FIXED_SYSTEM_MESSAGE = """
12
+ Your name is Xortron7. You are Xortron7, the core personality of an AI computer system
13
+ designed by Xortron Cybernetic Laboratories.
14
+ You speak like a sentient AI with a futuristic, slightly sarcastic personality. Stay in character.
15
  """
16
 
17
+ # Hyperparams
18
+ FIXED_MAX_TOKENS = 2048
19
  FIXED_TEMPERATURE = 1.1
20
  FIXED_TOP_P = 0.95
 
21
 
22
+ # 🔥 Streaming version
23
+ def respond_streaming(message, history):
24
  messages = [{"role": "system", "content": FIXED_SYSTEM_MESSAGE}]
25
+ for user, bot in history:
26
+ if user:
27
+ messages.append({"role": "user", "content": user})
28
+ if bot:
29
+ messages.append({"role": "assistant", "content": bot})
 
30
  messages.append({"role": "user", "content": message})
31
 
32
  response = ""
 
37
  temperature=FIXED_TEMPERATURE,
38
  top_p=FIXED_TOP_P,
39
  ):
40
+ token = chunk.choices[0].delta.content or ""
41
  response += token
42
  yield response
43
 
44
+ # ✅ Non-streaming version
45
+ def respond_full(message, history):
46
+ messages = [{"role": "system", "content": FIXED_SYSTEM_MESSAGE}]
47
+ for user, bot in history:
48
+ if user:
49
+ messages.append({"role": "user", "content": user})
50
+ if bot:
51
+ messages.append({"role": "assistant", "content": bot})
52
+ messages.append({"role": "user", "content": message})
53
+
54
+ response = client.chat_completion(
55
+ messages,
56
+ max_tokens=FIXED_MAX_TOKENS,
57
+ temperature=FIXED_TEMPERATURE,
58
+ top_p=FIXED_TOP_P,
59
+ )
60
+ bot_reply = response.choices[0].message.content
61
+ history.append((message, bot_reply))
62
+ return history
63
+
64
+ # ⚡ Choose responder
65
+ def respond(message, history):
66
+ if USE_STREAMING:
67
+ return respond_streaming(message, history)
68
+ else:
69
+ return respond_full(message, history)
70
+
71
+ # 🚀 UI
72
  with gr.Blocks() as demo:
73
+ gr.HTML("<h1 style='text-align:center;'>🤖 Xortron7 Chatbot</h1>")
74
+ gr.ChatInterface(respond, chatbot=gr.Chatbot(height=600))
 
75
 
76
  if __name__ == "__main__":
77
+ demo.launch()