prabinpanta0 commited on
Commit
7e061e2
1 Parent(s): 4e13c47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -36
app.py CHANGED
@@ -4,44 +4,48 @@ import vertexai.preview.generative_models as generative_models
4
  import gradio as gr
5
 
6
  def generate(text):
7
- vertexai.init(project="idyllic-now-424815-h2", location="us-central1")
8
- model = GenerativeModel(
9
- "gemini-1.5-flash-001",
10
- system_instruction=[
11
- 'Objective', text, 'Instructions', 'Use words like "thou," "thee," "thy," "henceforth," "forsooth," and "verily."',
12
- 'Transform sentences to be elaborate and formal.',
13
- 'Maintain a delusional nobleman tone, addressing others as if they are of lower status.',
14
- 'Examples', ':', 'Input', ': "Hey, what\'s up?"', 'Output', ': "Greetings, fair compatriot! What news dost thou bring?"',
15
- 'Input', ': "Can you help me with this?"', 'Output', ': "Might I entreat thee to lend thine esteemed assistance in this matter?"',
16
- 'Input', ': "I don\'t like this."', 'Output', ': "I find this matter to be most displeasing and beneath my esteemed tastes."',
17
- 'Input', ': "You did a good job."', 'Output', ': "Thy efforts are commendable, and thou hast performed admirably."',
18
- 'Input', ': "See you later."', 'Output', ': "Until we meet again, may fortune smile upon thee."',
19
- 'Keep the original meaning, but make it sound like a nobleman from a fantasy world.'
20
- ]
21
- )
22
- generation_config = {
23
- 'max_output_tokens': 3019,
24
- 'temperature': 1,
25
- 'top_p': 0.32,
26
- }
27
- safety_settings = {
28
- generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
29
- generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
30
- generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
31
- generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
32
- }
33
- responses = model.generate_content(
34
- [text],
35
- generation_config=generation_config,
36
- safety_settings=safety_settings,
37
- stream=True,
38
- )
 
39
 
40
- response_text = ""
41
- for response in responses:
42
- response_text += response.text
43
 
44
- return response_text
 
 
 
45
 
46
  iface = gr.Interface(
47
  fn=generate,
 
4
  import gradio as gr
5
 
6
  def generate(text):
7
+ try:
8
+ vertexai.init(project="idyllic-now-424815-h2", location="us-central1")
9
+ model = GenerativeModel(
10
+ "gemini-1.5-flash-001",
11
+ system_instruction=[
12
+ 'Objective', text, 'Instructions', 'Use words like "thou," "thee," "thy," "henceforth," "forsooth," and "verily."',
13
+ 'Transform sentences to be elaborate and formal.',
14
+ 'Maintain a delusional nobleman tone, addressing others as if they are of lower status.',
15
+ 'Examples', ':', 'Input', ': "Hey, what\'s up?"', 'Output', ': "Greetings, fair compatriot! What news dost thou bring?"',
16
+ 'Input', ': "Can you help me with this?"', 'Output', ': "Might I entreat thee to lend thine esteemed assistance in this matter?"',
17
+ 'Input', ': "I don\'t like this."', 'Output', ': "I find this matter to be most displeasing and beneath my esteemed tastes."',
18
+ 'Input', ': "You did a good job."', 'Output', ': "Thy efforts are commendable, and thou hast performed admirably."',
19
+ 'Input', ': "See you later."', 'Output', ': "Until we meet again, may fortune smile upon thee."',
20
+ 'Keep the original meaning, but make it sound like a nobleman from a fantasy world.'
21
+ ]
22
+ )
23
+ generation_config = {
24
+ 'max_output_tokens': 3019,
25
+ 'temperature': 1,
26
+ 'top_p': 0.32,
27
+ }
28
+ safety_settings = {
29
+ generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
30
+ generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
31
+ generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
32
+ generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
33
+ }
34
+ responses = model.generate_content(
35
+ [text],
36
+ generation_config=generation_config,
37
+ safety_settings=safety_settings,
38
+ stream=True,
39
+ )
40
 
41
+ response_text = ""
42
+ for response in responses:
43
+ response_text += response.text
44
 
45
+ return response_text
46
+
47
+ except Exception as e:
48
+ return str(e)
49
 
50
  iface = gr.Interface(
51
  fn=generate,