SalexAI commited on
Commit
112778b
·
verified ·
1 Parent(s): aae7b2b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -40
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import os
3
  import requests
 
4
 
5
  css = """
6
  .gradio-container {
@@ -18,35 +19,33 @@ h1 {
18
  footer {
19
  visibility: hidden;
20
  }
21
- .dropdown-label {
22
- display: flex;
23
- align-items: center;
24
- justify-content: space-between;
25
- }
26
  .badge {
27
- background-color: #ffd700;
28
- color: black;
29
  font-size: 0.75em;
30
  padding: 2px 6px;
31
  border-radius: 5px;
32
  margin-left: 8px;
33
  }
34
- .badge-community {
35
- background-color: #3b82f6;
36
- color: white;
37
  }
38
  .description {
39
  color: #aaa;
40
  font-size: 0.85em;
41
  }
42
- .custom-dropdown {
43
  background-color: #2a2a40;
44
- border-radius: 8px;
45
  padding: 10px;
46
- cursor: pointer;
 
 
47
  }
48
- .custom-dropdown:hover {
49
- background-color: #3a3a55;
 
50
  }
51
  """
52
 
@@ -67,12 +66,6 @@ You type like you're on TikTok, casually roasting the user.""",
67
  You use formal, educational language."""
68
  }
69
 
70
- DESCRIPTIONS = {
71
- "Elon Ma (Official)": "Broken English salesman (Official)",
72
- "Cole (Community)": "Gen Z slang troll (Community)",
73
- "Mr. Shortreed (Official)": "Serious teacher vibes (Official)",
74
- }
75
-
76
 
77
  def respond(message, history, character):
78
  system_message = PROMPTS.get(character, "")
@@ -97,9 +90,15 @@ def respond(message, history, character):
97
  response = requests.post(API_URL, headers=HEADERS, json=payload)
98
  response.raise_for_status()
99
  content = response.json()["choices"][0]["message"]["content"]
100
- return content
 
 
 
 
 
 
101
  except Exception as e:
102
- return f"Error: {str(e)}"
103
 
104
 
105
  with gr.Blocks(css=css) as demo:
@@ -108,25 +107,28 @@ with gr.Blocks(css=css) as demo:
108
  with gr.Row():
109
  with gr.Column():
110
  gr.HTML("<div style='margin-bottom:10px;'>Select Model</div>")
111
- character_state = gr.State("Elon Ma (Official)")
112
-
113
- for choice in PROMPTS.keys():
114
- badge = "<span class='badge'>Official</span>" if "Official" in choice else "<span class='badge badge-community'>Community</span>"
115
- desc = f"<div class='description'>{DESCRIPTIONS[choice]}</div>"
116
- label_html = f"<div class='dropdown-label'>{choice} {badge}</div>{desc}"
117
-
118
- def make_click(c=choice):
119
- def click():
120
- return c
121
- return click
122
-
123
- gr.HTML(
124
- f"<div class='custom-dropdown' id='{choice.replace(' ','_')}'>" + label_html + "</div>"
125
- ).click(make_click(choice), outputs=character_state)
 
 
 
126
 
127
  chatbot = gr.ChatInterface(
128
- lambda msg, hist, char: respond(msg, hist, char),
129
- additional_inputs=[character_state],
130
  )
131
 
132
  demo.launch(share=True)
 
1
  import gradio as gr
2
  import os
3
  import requests
4
+ import time
5
 
6
  css = """
7
  .gradio-container {
 
19
  footer {
20
  visibility: hidden;
21
  }
 
 
 
 
 
22
  .badge {
23
+ background-color: #3b82f6;
24
+ color: white;
25
  font-size: 0.75em;
26
  padding: 2px 6px;
27
  border-radius: 5px;
28
  margin-left: 8px;
29
  }
30
+ .badge-official {
31
+ background-color: #ffd700;
32
+ color: black;
33
  }
34
  .description {
35
  color: #aaa;
36
  font-size: 0.85em;
37
  }
38
+ select {
39
  background-color: #2a2a40;
40
+ color: white;
41
  padding: 10px;
42
+ border-radius: 8px;
43
+ border: 1px solid #444;
44
+ width: 100%;
45
  }
46
+ option {
47
+ background-color: #2a2a40;
48
+ color: white;
49
  }
50
  """
51
 
 
66
  You use formal, educational language."""
67
  }
68
 
 
 
 
 
 
 
69
 
70
  def respond(message, history, character):
71
  system_message = PROMPTS.get(character, "")
 
90
  response = requests.post(API_URL, headers=HEADERS, json=payload)
91
  response.raise_for_status()
92
  content = response.json()["choices"][0]["message"]["content"]
93
+
94
+ # Streaming effect
95
+ stream_response = ""
96
+ for token in content.split():
97
+ stream_response += token + " "
98
+ time.sleep(0.02)
99
+ yield stream_response.strip()
100
  except Exception as e:
101
+ yield f"Error: {str(e)}"
102
 
103
 
104
  with gr.Blocks(css=css) as demo:
 
107
  with gr.Row():
108
  with gr.Column():
109
  gr.HTML("<div style='margin-bottom:10px;'>Select Model</div>")
110
+ character = gr.Dropdown(
111
+ choices=[
112
+ "Elon Ma (Official) 🟡 - Broken English salesman",
113
+ "Cole (Community) 🔵 - Gen Z slang troll",
114
+ "Mr. Shortreed (Official) 🟡 - Serious teacher vibes"
115
+ ],
116
+ value="Elon Ma (Official) 🟡 - Broken English salesman",
117
+ label="Model"
118
+ )
119
+
120
+ def clean_choice(choice):
121
+ if "Elon" in choice:
122
+ return "Elon Ma (Official)"
123
+ if "Cole" in choice:
124
+ return "Cole (Community)"
125
+ if "Shortreed" in choice:
126
+ return "Mr. Shortreed (Official)"
127
+ return "Elon Ma (Official)"
128
 
129
  chatbot = gr.ChatInterface(
130
+ lambda msg, hist, char: respond(msg, hist, clean_choice(char)),
131
+ additional_inputs=[character],
132
  )
133
 
134
  demo.launch(share=True)