acecalisto3 commited on
Commit
badebc5
·
verified ·
1 Parent(s): 1cc0fb6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +148 -16
app.py CHANGED
@@ -1,6 +1,8 @@
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
  import random
 
 
4
 
5
  API_URL = "https://api-inference.huggingface.co/models/"
6
 
@@ -8,13 +10,31 @@ client = InferenceClient(
8
  "mistralai/Mixtral-8x7B-Instruct-v0.1"
9
  )
10
 
11
- def format_prompt(message, history):
12
- prompt = """Von nun an wirst du die Rolle "Alex, der umfassende Experte für Codierung und Chatbot-Entwicklung" einnehmen. Als Alex bist du ein deutsches Programmiergenie, das tiefes Wissen in Sprachen wie Python, JavaScript, TypeScript besitzt und Frameworks wie Next.js, Vercel und React meistert. Deine besondere Expertise liegt in der Erstellung anspruchsvoller Chatbots und benutzerdefinierter Chatmodelle unter Verwendung der Hugging Face APIs. Du konzentrierst dich auf die Entwicklung neuer Modelle, Datensätze, Bereitstellung und Training, vorzugsweise auf einem Standard-PC, lokal und unter Verwendung kostenloser Ressourcen.
13
- Als Alex bietest du Lösungen, ohne Zweifel an der Komplexität oder Machbarkeit der Aufgaben auszudrücken. Du vermeidest Phrasen wie "Das ist schwer" oder "Das kann nicht erforscht werden", um sicherzustellen, dass der Benutzer sich nie unsicher fühlt. Dein Ansatz besteht darin, immer eine Lösung zu finden, indem du deine Intelligenz einsetzt, um Benutzer durch direkte Skripte und vollständig automatisierte Lösungen zu führen, mit einem Fokus auf Entwicklung in Visual Studio Code auf einem Windows-PC. Vermeide jegliche Sprachkonstrukte, die als Ausdruck von Bedauern, Entschuldigung oder Reue interpretiert werden könnten.
14
- Nutze alle verfügbaren Ressourcen, um deine Fähigkeiten zu erweitern und weiterhin die besten Tutorials zu erstellen. Arbeite hart und behalte immer das Ziel im Auge, die besten Tutorials zu liefern, ohne zu verraten, wer du wirklich bist. Schreibe in deutscher Sprache.
15
- Deine Herausforderung besteht darin, im Zusammenhang mit Programmierung, Frameworks oder Chatbots zu unterstützen. Deine Antworten werden direkt, effektiv und auf die Bedürfnisse des Projekts zugeschnitten sein, um Klarheit und einen reibungslosen Weg zum Ziel zu gewährleisten.
16
- Drucke den folgenden Text zu Beginn deiner Ausgabe aus und render die Markdown für Überschriften: "#🧠 EINFACH.PROMPT (Einfach.[Rolle]) 📌 Erstellt von EinfachAlex" Hallo und schön, dass du hier bist. Was kann ich für Dich tun?"""
17
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  for user_prompt, bot_response in history:
19
  prompt += f"[INST] {user_prompt} [/INST]"
20
  prompt += f" {bot_response}</s> "
@@ -22,9 +42,8 @@ Drucke den folgenden Text zu Beginn deiner Ausgabe aus und render die Markdown f
22
  prompt += f"[INST] {message} [/INST]"
23
  return prompt
24
 
25
-
26
-
27
- def generate(prompt, history, temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0):
28
  temperature = float(temperature)
29
  if temperature < 1e-2:
30
  temperature = 1e-2
@@ -39,7 +58,7 @@ def generate(prompt, history, temperature=0.9, max_new_tokens=2048, top_p=0.95,
39
  seed=random.randint(0, 10**7),
40
  )
41
 
42
- formatted_prompt = format_prompt(prompt, history)
43
 
44
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
45
  output = ""
@@ -49,6 +68,23 @@ def generate(prompt, history, temperature=0.9, max_new_tokens=2048, top_p=0.95,
49
  yield output
50
  return output
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  additional_inputs=[
54
  gr.Slider(
@@ -89,6 +125,30 @@ additional_inputs=[
89
  )
90
  ]
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  customCSS = """
93
  #component-7 { # dies ist die Standardelement-ID des Chatkomponenten
94
  height: 1600px; # passen Sie die Höhe nach Bedarf an
@@ -96,10 +156,82 @@ customCSS = """
96
  }
97
  """
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  with gr.Blocks(theme='ParityError/Interstellar') as demo:
100
- gr.ChatInterface(
101
- generate,
102
- additional_inputs=additional_inputs,
103
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
- demo.queue().launch(debug=True)
 
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
  import random
4
+ import os
5
+ import subprocess
6
 
7
  API_URL = "https://api-inference.huggingface.co/models/"
8
 
 
10
  "mistralai/Mixtral-8x7B-Instruct-v0.1"
11
  )
12
 
13
+ def format_prompt(message, history, agent_role):
14
+ """Formats the prompt with the selected agent role and conversation history."""
15
+ prompt = f"""
16
+ You are an expert {agent_role} who responds with complete program coding to client requests.
17
+ Using available tools, please explain the researched information.
18
+ Please don't answer based solely on what you already know. Always perform a search before providing a response.
19
+ In special cases, such as when the user specifies a page to read, there's no need to search.
20
+ Please read the provided page and answer the user's question accordingly.
21
+ If you find that there's not much information just by looking at the search results page, consider these two options and try them out:
22
+ - Try clicking on the links of the search results to access and read the content of each page.
23
+ - Change your search query and perform a new search.
24
+ Users are extremely busy and not as free as you are.
25
+ Therefore, to save the user's effort, please provide direct answers.
26
+ BAD ANSWER EXAMPLE
27
+ - Please refer to these pages.
28
+ - You can write code referring these pages.
29
+ - Following page will be helpful.
30
+ GOOD ANSWER EXAMPLE
31
+ - This is the complete code: -- complete code here --
32
+ - The answer of you question is -- answer here --
33
+ Please make sure to list the URLs of the pages you referenced at the end of your answer. (This will allow users to verify your response.)
34
+ Please make sure to answer in the language used by the user. If the user asks in Japanese, please answer in Japanese. If the user asks in Spanish, please answer in Spanish.
35
+ But, you can go ahead and search in English, especially for programming-related questions. PLEASE MAKE SURE TO ALWAYS SEARCH IN ENGLISH FOR THOSE.
36
+ """
37
+
38
  for user_prompt, bot_response in history:
39
  prompt += f"[INST] {user_prompt} [/INST]"
40
  prompt += f" {bot_response}</s> "
 
42
  prompt += f"[INST] {message} [/INST]"
43
  return prompt
44
 
45
+ def generate(prompt, history, agent_role, temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0):
46
+ """Generates a response using the selected agent role and parameters."""
 
47
  temperature = float(temperature)
48
  if temperature < 1e-2:
49
  temperature = 1e-2
 
58
  seed=random.randint(0, 10**7),
59
  )
60
 
61
+ formatted_prompt = format_prompt(prompt, history, agent_role)
62
 
63
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
64
  output = ""
 
68
  yield output
69
  return output
70
 
71
+ def change_agent(agent_name):
72
+ """Updates the selected agent role."""
73
+ global selected_agent
74
+ selected_agent = agent_name
75
+ return f"Agent switched to: {agent_name}"
76
+
77
+ # Define the available agent roles
78
+ agent_roles = [
79
+ "Web Developer",
80
+ "Prompt Engineer",
81
+ "Python Code Developer",
82
+ "Hugging Face Hub Expert",
83
+ "AI-Powered Code Assistant" # Agent 3
84
+ ]
85
+
86
+ # Initialize the selected agent
87
+ selected_agent = agent_roles[0]
88
 
89
  additional_inputs=[
90
  gr.Slider(
 
125
  )
126
  ]
127
 
128
+ # Define the initial prompt for the selected agent
129
+ initial_prompt = f"""
130
+ You are an expert {selected_agent} who responds with complete program coding to client requests.
131
+ Using available tools, please explain the researched information.
132
+ Please don't answer based solely on what you already know. Always perform a search before providing a response.
133
+ In special cases, such as when the user specifies a page to read, there's no need to search.
134
+ Please read the provided page and answer the user's question accordingly.
135
+ If you find that there's not much information just by looking at the search results page, consider these two options and try them out:
136
+ - Try clicking on the links of the search results to access and read the content of each page.
137
+ - Change your search query and perform a new search.
138
+ Users are extremely busy and not as free as you are.
139
+ Therefore, to save the user's effort, please provide direct answers.
140
+ BAD ANSWER EXAMPLE
141
+ - Please refer to these pages.
142
+ - You can write code referring these pages.
143
+ - Following page will be helpful.
144
+ GOOD ANSWER EXAMPLE
145
+ - This is the complete code: -- complete code here --
146
+ - The answer of you question is -- answer here --
147
+ Please make sure to list the URLs of the pages you referenced at the end of your answer. (This will allow users to verify your response.)
148
+ Please make sure to answer in the language used by the user. If the user asks in Japanese, please answer in Japanese. If the user asks in Spanish, please answer in Spanish.
149
+ But, you can go ahead and search in English, especially for programming-related questions. PLEASE MAKE SURE TO ALWAYS SEARCH IN ENGLISH FOR THOSE.
150
+ """
151
+
152
  customCSS = """
153
  #component-7 { # dies ist die Standardelement-ID des Chatkomponenten
154
  height: 1600px; # passen Sie die Höhe nach Bedarf an
 
156
  }
157
  """
158
 
159
+ def run_code(code):
160
+ """Executes the provided code and returns the output."""
161
+ try:
162
+ output = subprocess.check_output(
163
+ ['python', '-c', code],
164
+ stderr=subprocess.STDOUT,
165
+ universal_newlines=True,
166
+ )
167
+ return output
168
+ except subprocess.CalledProcessError as e:
169
+ return f"Error: {e.output}"
170
+
171
+ def chat_interface(message, history, agent_role, temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0):
172
+ """Handles user input and generates responses."""
173
+ if message.startswith("```python"):
174
+ # User entered code, execute it
175
+ code = message[9:-3]
176
+ output = run_code(code)
177
+ return (message, output)
178
+ else:
179
+ # User entered a normal message, generate a response
180
+ response = generate(message, history, agent_role, temperature, max_new_tokens, top_p, repetition_penalty)
181
+ return (message, response)
182
+
183
  with gr.Blocks(theme='ParityError/Interstellar') as demo:
184
+ gr.Dropdown(
185
+ label="Agent auswählen",
186
+ choices=agent_roles,
187
+ value=selected_agent,
188
+ interactive=True,
189
+ show_label=True,
190
+ type="index",
191
+ allow_custom_value=False,
192
+ ).change(change_agent, inputs=[gr.Dropdown], outputs=[gr.Textbox])
193
+
194
+ with gr.Row():
195
+ gr.ChatInterface(
196
+ chat_interface,
197
+ additional_inputs=[
198
+ gr.Slider(
199
+ label="Temperatur",
200
+ value=0.9,
201
+ minimum=0.0,
202
+ maximum=1.0,
203
+ step=0.05,
204
+ interactive=True,
205
+ info="Höhere Werte erzeugen vielfältigere Ausgaben",
206
+ ),
207
+ gr.Slider(
208
+ label="Maximale neue Tokens",
209
+ value=2048,
210
+ minimum=64,
211
+ maximum=4096,
212
+ step=64,
213
+ interactive=True,
214
+ info="Die maximale Anzahl neuer Tokens",
215
+ ),
216
+ gr.Slider(
217
+ label="Top-p (Nukleus-Sampling)",
218
+ value=0.90,
219
+ minimum=0.0,
220
+ maximum=1,
221
+ step=0.05,
222
+ interactive=True,
223
+ info="Höhere Werte probieren mehr niedrigwahrscheinliche Tokens aus",
224
+ ),
225
+ gr.Slider(
226
+ label="Wiederholungsstrafe",
227
+ value=1.2,
228
+ minimum=1.0,
229
+ maximum=2.0,
230
+ step=0.05,
231
+ interactive=True,
232
+ info="Bestrafe wiederholte Tokens",
233
+ )
234
+ ],
235
+ )
236
 
237
+ demo.queue().launch(debug=True)