Mike014 commited on
Commit
cc19484
·
verified ·
1 Parent(s): ff7cd01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -19
app.py CHANGED
@@ -1,12 +1,25 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
- """
5
- 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
6
- """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
9
 
 
 
 
 
 
 
 
 
 
 
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
@@ -16,15 +29,14 @@ def respond(
16
  top_p,
17
  ):
18
  messages = [{"role": "system", "content": system_message}]
19
-
20
  for val in history:
21
  if val[0]:
22
  messages.append({"role": "user", "content": val[0]})
23
  if val[1]:
24
  messages.append({"role": "assistant", "content": val[1]})
25
-
26
  messages.append({"role": "user", "content": message})
27
-
28
  response = ""
29
 
30
  for message in client.chat_completion(
@@ -35,30 +47,72 @@ def respond(
35
  top_p=top_p,
36
  ):
37
  token = message.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
- demo = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
  ],
 
 
60
  )
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
 
 
 
 
 
 
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import csv
4
+ import os
5
 
6
+ # Initialize the client with the open-source AI model
 
 
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
+ # Files for storing tested prompts and feedback
10
+ PROMPT_HISTORY_FILE = "prompt_history.csv"
11
+ FEEDBACK_FILE = "feedback.csv"
12
 
13
+ # Function to save prompts and responses in a CSV file
14
+ def log_prompt(prompt, response):
15
+ file_exists = os.path.isfile(PROMPT_HISTORY_FILE)
16
+ with open(PROMPT_HISTORY_FILE, mode="a", newline="", encoding="utf-8") as file:
17
+ writer = csv.writer(file)
18
+ if not file_exists:
19
+ writer.writerow(["Prompt", "Response"])
20
+ writer.writerow([prompt, response])
21
+
22
+ # Function to test prompts and store responses
23
  def respond(
24
  message,
25
  history: list[tuple[str, str]],
 
29
  top_p,
30
  ):
31
  messages = [{"role": "system", "content": system_message}]
32
+
33
  for val in history:
34
  if val[0]:
35
  messages.append({"role": "user", "content": val[0]})
36
  if val[1]:
37
  messages.append({"role": "assistant", "content": val[1]})
38
+
39
  messages.append({"role": "user", "content": message})
 
40
  response = ""
41
 
42
  for message in client.chat_completion(
 
47
  top_p=top_p,
48
  ):
49
  token = message.choices[0].delta.content
 
50
  response += token
51
  yield response
52
 
53
+ # Save the prompt and response
54
+ log_prompt(message, response)
55
+
56
+ # Function to generate advanced prompts with different prompting techniques
57
+ def generate_prompt(prompt, technique):
58
+ if technique == "Zero-shot":
59
+ return prompt
60
+ elif technique == "Few-shot":
61
+ return f"Example 1: Input: 'Good morning', Output: 'Hello!'\nExample 2: Input: 'How are you?', Output: 'I'm fine, thank you!'\nNow generate a response for: '{prompt}'"
62
+ elif technique == "Chain-of-Thought":
63
+ return f"Step 1: Analyze the question '{prompt}'\nStep 2: Break down each element separately\nStep 3: Provide a detailed response."
64
+ else:
65
+ return prompt
66
+
67
+ # Function to collect feedback on AI responses
68
+ def rate_response(prompt, response, rating):
69
+ with open(FEEDBACK_FILE, mode="a", newline="", encoding="utf-8") as file:
70
+ writer = csv.writer(file)
71
+ writer.writerow([prompt, response, rating])
72
+ return "Thank you for your feedback! Your rating has been recorded."
73
 
74
+ # Main UI for testing Prompt Engineering
75
+ chat_interface = gr.ChatInterface(
 
 
76
  respond,
77
  additional_inputs=[
78
+ gr.Textbox(value="You are a friendly chatbot.", label="System message"),
79
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
80
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
81
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
82
  ],
83
+ title="Prompt Engineering Lab",
84
+ description="Test your prompts with an open-source AI model and analyze the results.",
85
  )
86
 
87
+ # UI for selecting advanced prompting techniques
88
+ prompt_interface = gr.Interface(
89
+ fn=generate_prompt,
90
+ inputs=[gr.Textbox(label="Prompt"), gr.Radio(["Zero-shot", "Few-shot", "Chain-of-Thought"], label="Technique")],
91
+ outputs="text",
92
+ title="Prompt Engineering Techniques",
93
+ description="Select an advanced technique to generate more effective prompts.",
94
+ )
95
+
96
+ # UI for collecting feedback on AI responses
97
+ rating_interface = gr.Interface(
98
+ fn=rate_response,
99
+ inputs=[
100
+ gr.Textbox(label="Prompt"),
101
+ gr.Textbox(label="AI Output"),
102
+ gr.Slider(1, 5, step=1, label="Rating (1-5)"),
103
+ ],
104
+ outputs="text",
105
+ title="Prompt Quality Evaluation",
106
+ description="Rate the quality of the AI response to improve the prompts.",
107
+ )
108
 
109
+ # Creating a tabbed interface with all components
110
+ demo = gr.TabbedInterface(
111
+ [chat_interface, prompt_interface, rating_interface],
112
+ ["AI Chat", "Prompt Engineering", "Output Evaluation"]
113
+ )
114
+
115
+ # Launch the UI
116
  if __name__ == "__main__":
117
  demo.launch()
118
+