randeom commited on
Commit
47bcb45
1 Parent(s): e5bfe36

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -0
app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+
4
+ client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
5
+
6
+ def format_prompt(message, job_profession):
7
+ system_prompt = f"You are a People Generator. You generate People based on user input, always in the following Format: You are (Name) (Age) old from (Country), you are an expert in the field {job_profession}. It is very IMPORTANT! that you response always in the given Format. Do NOT response with I am! Always Start with You are!"
8
+ prompt = f"<s>[SYS] {system_prompt} [/SYS][INST] {message} [/INST]</s>"
9
+ return prompt
10
+
11
+ def generate(message, job_profession, temperature=0.9, max_new_tokens=8192, top_p=0.95, repetition_penalty=1.0):
12
+ # Parse the input to determine job profession based on the presence of a message.
13
+ actual_job = message if message else job_profession
14
+
15
+ temperature = float(temperature)
16
+ if temperature < 1e-2:
17
+ temperature = 1e-2
18
+ top_p = float(top_p)
19
+
20
+ generate_kwargs = {
21
+ "temperature": temperature,
22
+ "max_new_tokens": max_new_tokens,
23
+ "top_p": top_p,
24
+ "repetition_penalty": repetition_penalty,
25
+ "do_sample": True,
26
+ "seed": 42,
27
+ }
28
+
29
+ formatted_prompt = format_prompt("", actual_job) # message should be empty here if we're using job from input
30
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
31
+ output = ""
32
+
33
+ for response in stream:
34
+ output += response.token.text
35
+ yield output
36
+
37
+ css = """
38
+ #mkd {
39
+ height: 500px;
40
+ overflow: auto;
41
+ border: 1px solid #ccc;
42
+ }
43
+ """
44
+
45
+ with gr.Blocks(css=css) as demo:
46
+ with gr.Row():
47
+ with gr.Column(scale=2):
48
+ gr.HTML("<h1>Settings</h1>")
49
+ job_dropdown = gr.Dropdown(
50
+ label="Choose a job profession",
51
+ choices=["Relationship Expert", "Sales Manager", "Blue Team Hacker", "Senior Web Developer", "C++ Developer", "Article Author", "News Anchor", "Finance Advisor"],
52
+ value="Relationship Expert" # Initial value
53
+ )
54
+ with gr.Column(scale=3):
55
+ gr.HTML("<h1><center>GPT Prompt Generator<h1><center>")
56
+ message_input = gr.Textbox(label="Input your message (overrides job dropdown)", placeholder="Type a job profession here to override the dropdown...")
57
+ generate_button = gr.Button("Generate")
58
+ output_area = gr.Textbox(label="AI Response", interactive=False, lines=10)
59
+ generate_button.click(
60
+ fn=generate,
61
+ inputs=[message_input, job_dropdown],
62
+ outputs=output_area
63
+ )
64
+
65
+ demo.launch(debug=True)
66
+