prabinpanta0 commited on
Commit
1d51e90
1 Parent(s): 70a4536

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
8
+ # Read the service account key JSON file path from environment variable
9
+ SERVICE_ACCOUNT_KEY_PATH = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
10
+
11
+ if not SERVICE_ACCOUNT_KEY_PATH:
12
+ raise ValueError("The GOOGLE_APPLICATION_CREDENTIALS environment variable is not set.")
13
+
14
+ with open(SERVICE_ACCOUNT_KEY_PATH) as f:
15
+ service_account_info = json.load(f)
16
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = SERVICE_ACCOUNT_KEY_PATH
17
+
18
+ def generate(text):
19
+ try:
20
+ vertexai.init(project="idyllic-now-424815-h2", location="us-central1")
21
+ model = GenerativeModel(
22
+ "gemini-1.5-flash-001",
23
+ system_instruction=[
24
+ 'Objective', text, 'Instructions', """You are an AI model designed to provide concise information about big data analytics across various fields without mentioning the question. Respond with a focused, one-line answer that captures the essence of the key risk, benefit, or trend associated with the topic.
25
+
26
+
27
+ input: What do you consider the most significant risk of over-reliance on big data analytics in stock market risk management?
28
+ output: Increased market volatility.
29
+
30
+ input: What is a major benefit of big data analytics in healthcare?
31
+ output: Enhanced patient care through personalized treatment.
32
+
33
+ input: What is a key challenge of big data analytics in retail?
34
+ output: Maintaining data privacy and security.
35
+
36
+ input: What is a primary advantage of big data analytics in manufacturing?
37
+ output: Improved production efficiency and predictive maintenance.
38
+
39
+ input: What is a significant risk associated with big data analytics in education?
40
+ output: Potential widening of the achievement gap if data is not used equitably. """
41
+
42
+ ]
43
+ )
44
+ generation_config = {
45
+ 'max_output_tokens': 3019,
46
+ 'temperature': 1,
47
+ 'top_p': 0.32,
48
+ }
49
+ safety_settings = {
50
+ generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_NONE,
51
+ generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_NONE,
52
+ generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_NONE,
53
+ generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_NONE,
54
+ }
55
+ responses = model.generate_content(
56
+ [text],
57
+ generation_config=generation_config,
58
+ safety_settings=safety_settings,
59
+ stream=True,
60
+ )
61
+
62
+ response_text = ""
63
+ for response in responses:
64
+ response_text += response.text
65
+
66
+ return response_text if response_text else "No valid response generated or response was blocked."
67
+
68
+ except Exception as e:
69
+ return str(e)
70
+
71
+ # # Custom HTML and JavaScript for "Copy to Clipboard" functionality
72
+ # js = """
73
+
74
+ # function copyToClipboard() {
75
+ # var copyText = document.getElementById("output-textbox");
76
+ # copyText.select();
77
+ # document.execCommand("copy");
78
+ # }
79
+ # """
80
+
81
+ iface = gr.Interface(
82
+ fn=generate,
83
+
84
+ inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
85
+ outputs="text",
86
+ title="Chuunibyou Text Generator",
87
+ description="Transform text into an elaborate and formal style with a nobleman tone.",
88
+ live=False
89
+ )
90
+
91
+ def launch_custom_interface():
92
+ iface.launch()
93
+ with gr.TabbedInterface(fn=generate, inputs=gr.Textbox(lines=2, placeholder="Enter text here..."), outputs=gr.HTML(label="Output")) as ti:
94
+ ti.add(custom_html)
95
+
96
+ if __name__ == "__main__":
97
+ launch_custom_interface()