prabinpanta0 commited on
Commit
cdadfb1
·
verified ·
1 Parent(s): 49e8c82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py CHANGED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import vertexai
4
+ from vertexai.generative_models import GenerativeModel
5
+ import vertexai.preview.generative_models as generative_models
6
+ import gradio as gr
7
+ # Read the service account key JSON file path from environment variable
8
+ SERVICE_ACCOUNT_KEY_PATH = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
9
+
10
+ if not SERVICE_ACCOUNT_KEY_PATH:
11
+ raise ValueError("The GOOGLE_APPLICATION_CREDENTIALS environment variable is not set.")
12
+
13
+ with open(SERVICE_ACCOUNT_KEY_PATH) as f:
14
+ service_account_info = json.load(f)
15
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = SERVICE_ACCOUNT_KEY_PATH
16
+
17
+ def generate(text):
18
+ try:
19
+ vertexai.init(project="idyllic-now-424815-h2", location="us-central1")
20
+ model = GenerativeModel(
21
+ "gemini-1.5-flash-001",
22
+ system_instruction=[
23
+ 'Objective', text, 'Instructions', 'Use words like "thou," "thee," "thy," "henceforth," "forsooth," and "verily."',
24
+ 'Transform sentences to be elaborate and formal.',
25
+ 'Maintain a delusional nobleman tone, addressing others as if they are of lower status.',
26
+ 'Examples', ':', 'Input', ': "Hey, what\'s up?"', 'Output', ': "Greetings, fair compatriot! What news dost thou bring?"',
27
+ 'Input', ': "Can you help me with this?"', 'Output', ': "Might I entreat thee to lend thine esteemed assistance in this matter?"',
28
+ 'Input', ': "I don\'t like this."', 'Output', ': "I find this matter to be most displeasing and beneath my esteemed tastes."',
29
+ 'Input', ': "You did a good job."', 'Output', ': "Thy efforts are commendable, and thou hast performed admirably."',
30
+ 'Input', ': "See you later."', 'Output', ': "Until we meet again, may fortune smile upon thee."',
31
+ 'Keep the original meaning, but make it sound like a nobleman from a fantasy world.'
32
+ ]
33
+ )
34
+ generation_config = {
35
+ 'max_output_tokens': 3019,
36
+ 'temperature': 1,
37
+ 'top_p': 0.32,
38
+ }
39
+ safety_settings = {
40
+ generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_NONE,
41
+ generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_NONE,
42
+ generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_NONE,
43
+ generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_NONE,
44
+ }
45
+ responses = model.generate_content(
46
+ [text],
47
+ generation_config=generation_config,
48
+ safety_settings=safety_settings,
49
+ stream=True,
50
+ )
51
+
52
+ response_text = ""
53
+ for response in responses:
54
+ response_text += response.text
55
+
56
+ return response_text if response_text else "No valid response generated or response was blocked."
57
+
58
+ except Exception as e:
59
+ return str(e)
60
+
61
+ iface = gr.Interface(
62
+ fn=generate,
63
+ inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
64
+ outputs="text",
65
+ title="Chuunibyou Text Generator",
66
+ description="Transform text into an elaborate and formal style with a Chuunibyou tone."
67
+ )
68
+
69
+ if __name__ == "__main__":
70
+ iface.launch()