NexusInstruments commited on
Commit
ff2c847
·
verified ·
1 Parent(s): 6c94514

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -54
app.py CHANGED
@@ -1,70 +1,153 @@
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
 
19
- messages = [{"role": "system", "content": system_message}]
 
 
 
20
 
21
- messages.extend(history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  messages.append({"role": "user", "content": message})
24
 
25
- response = ""
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
 
 
 
 
30
  stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
  ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- type="messages",
49
- additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
- ),
60
- ],
61
- )
62
-
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
 
 
 
68
 
69
  if __name__ == "__main__":
 
70
  demo.launch()
 
1
+ import os
2
+ import json
3
  import gradio as gr
4
  from huggingface_hub import InferenceClient
5
 
6
+ # =========================================
7
+ # Configuration
8
+ # =========================================
9
 
10
+ DEFAULT_MODEL = "openai/gpt-oss-20b"
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ MODEL_OPTIONS = {
13
+ "Fast (Mistral 7B)": "mistralai/Mistral-7B-Instruct-v0.2",
14
+ "Balanced (GPT-OSS 20B)": "openai/gpt-oss-20b",
15
+ }
16
 
17
+ PERSONA_PRESETS = {
18
+ "Balanced Assistant": "You are a helpful, intelligent, and clear AI assistant.",
19
+ "Creative": "You are a highly creative and imaginative assistant.",
20
+ "Precise": "You are a concise, factual, and analytical assistant.",
21
+ "Code Expert": "You are a senior software engineer who writes clean, well-explained code.",
22
+ }
23
+
24
+ # =========================================
25
+ # Core Chat Logic
26
+ # =========================================
27
+
28
+ def stream_response(message, history, model_label, persona_label):
29
+ if not message:
30
+ yield history
31
+ return
32
+
33
+ model_name = MODEL_OPTIONS[model_label]
34
+ system_prompt = PERSONA_PRESETS[persona_label]
35
+
36
+ hf_token = os.environ.get("HF_TOKEN")
37
+ client = InferenceClient(model=model_name, token=hf_token)
38
+
39
+ messages = [{"role": "system", "content": system_prompt}]
40
+
41
+ for user_msg, assistant_msg in history:
42
+ messages.append({"role": "user", "content": user_msg})
43
+ messages.append({"role": "assistant", "content": assistant_msg})
44
 
45
  messages.append({"role": "user", "content": message})
46
 
47
+ history.append((message, ""))
48
 
49
+ partial_response = ""
50
+
51
+ for chunk in client.chat_completion(
52
+ messages=messages,
53
+ max_tokens=800,
54
+ temperature=0.7,
55
+ top_p=0.95,
56
  stream=True,
 
 
57
  ):
58
+ if chunk.choices and chunk.choices[0].delta.content:
59
+ token = chunk.choices[0].delta.content
60
+ partial_response += token
61
+ history[-1] = (message, partial_response)
62
+ yield history
63
+
64
+ # =========================================
65
+ # Utility Functions
66
+ # =========================================
67
+
68
+ def clear_chat():
69
+ return []
70
+
71
+ def export_chat(history):
72
+ return json.dumps(
73
+ [{"user": u, "assistant": a} for u, a in history],
74
+ indent=2
75
+ )
76
+
77
+ # =========================================
78
+ # UI Layout
79
+ # =========================================
80
+
81
+ with gr.Blocks(theme=gr.themes.Soft(), title="Omniscient IRIS") as demo:
82
+
83
+ gr.Markdown("## Omniscient IRIS — Public General AI")
84
+
85
+ with gr.Row():
86
+ with gr.Column(scale=3):
87
+ chatbot = gr.Chatbot(height=500)
88
+ msg = gr.Textbox(
89
+ placeholder="Type your message and press Enter...",
90
+ show_label=False
91
+ )
92
+
93
+ with gr.Row():
94
+ submit_btn = gr.Button("Send", variant="primary")
95
+ stop_btn = gr.Button("Stop")
96
+ clear_btn = gr.Button("Clear")
97
+
98
+ with gr.Column(scale=1):
99
+ model_selector = gr.Dropdown(
100
+ choices=list(MODEL_OPTIONS.keys()),
101
+ value="Balanced (GPT-OSS 20B)",
102
+ label="Model"
103
+ )
104
+
105
+ persona_selector = gr.Dropdown(
106
+ choices=list(PERSONA_PRESETS.keys()),
107
+ value="Balanced Assistant",
108
+ label="Persona"
109
+ )
110
+
111
+ export_btn = gr.Button("Export Conversation")
112
+
113
+ state = gr.State([])
114
+
115
+ # Submit events
116
+ submit_event = submit_btn.click(
117
+ stream_response,
118
+ inputs=[msg, state, model_selector, persona_selector],
119
+ outputs=chatbot,
120
+ )
121
+
122
+ msg.submit(
123
+ stream_response,
124
+ inputs=[msg, state, model_selector, persona_selector],
125
+ outputs=chatbot,
126
+ )
127
+
128
+ # Stop streaming
129
+ stop_btn.click(fn=None, cancels=[submit_event])
130
+
131
+ # Clear
132
+ clear_btn.click(
133
+ fn=clear_chat,
134
+ outputs=chatbot,
135
+ ).then(
136
+ lambda: [],
137
+ outputs=state
138
+ )
139
+
140
+ # Export
141
+ export_btn.click(
142
+ export_chat,
143
+ inputs=chatbot,
144
+ outputs=gr.File(label="Download JSON")
145
+ )
146
 
147
+ # =========================================
148
+ # Launch
149
+ # =========================================
150
 
151
  if __name__ == "__main__":
152
+ demo.queue()
153
  demo.launch()