chuunibyou / app_FORM.py
prabinpanta0's picture
Rename app.py to app_FORM.py
560b1b9 verified
raw
history blame contribute delete
No virus
3.83 kB
import os
import json
import vertexai
from vertexai.generative_models import GenerativeModel
import vertexai.preview.generative_models as generative_models
import gradio as gr
# Read the service account key JSON file path from environment variable
SERVICE_ACCOUNT_KEY_PATH = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
if not SERVICE_ACCOUNT_KEY_PATH:
raise ValueError("The GOOGLE_APPLICATION_CREDENTIALS environment variable is not set.")
with open(SERVICE_ACCOUNT_KEY_PATH) as f:
service_account_info = json.load(f)
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = SERVICE_ACCOUNT_KEY_PATH
def generate(text):
try:
vertexai.init(project="idyllic-now-424815-h2", location="us-central1")
model = GenerativeModel(
"gemini-1.5-flash-001",
system_instruction=[
'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.
input: What do you consider the most significant risk of over-reliance on big data analytics in stock market risk management?
output: Increased market volatility.
input: What is a major benefit of big data analytics in healthcare?
output: Enhanced patient care through personalized treatment.
input: What is a key challenge of big data analytics in retail?
output: Maintaining data privacy and security.
input: What is a primary advantage of big data analytics in manufacturing?
output: Improved production efficiency and predictive maintenance.
input: What is a significant risk associated with big data analytics in education?
output: Potential widening of the achievement gap if data is not used equitably. """
]
)
generation_config = {
'max_output_tokens': 3019,
'temperature': 1,
'top_p': 0.32,
}
safety_settings = {
generative_models.HarmCategory.HARM_CATEGORY_HATE_SPEECH: generative_models.HarmBlockThreshold.BLOCK_NONE,
generative_models.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: generative_models.HarmBlockThreshold.BLOCK_NONE,
generative_models.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: generative_models.HarmBlockThreshold.BLOCK_NONE,
generative_models.HarmCategory.HARM_CATEGORY_HARASSMENT: generative_models.HarmBlockThreshold.BLOCK_NONE,
}
responses = model.generate_content(
[text],
generation_config=generation_config,
safety_settings=safety_settings,
stream=True,
)
response_text = ""
for response in responses:
response_text += response.text
return response_text if response_text else "No valid response generated or response was blocked."
except Exception as e:
return str(e)
# # Custom HTML and JavaScript for "Copy to Clipboard" functionality
# js = """
# function copyToClipboard() {
# var copyText = document.getElementById("output-textbox");
# copyText.select();
# document.execCommand("copy");
# }
# """
iface = gr.Interface(
fn=generate,
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
outputs="text",
title="Chuunibyou Text Generator",
description="Transform text into an elaborate and formal style with a nobleman tone.",
live=False
)
def launch_custom_interface():
iface.launch()
with gr.TabbedInterface(fn=generate, inputs=gr.Textbox(lines=2, placeholder="Enter text here..."), outputs=gr.HTML(label="Output")) as ti:
ti.add(custom_html)
if __name__ == "__main__":
launch_custom_interface()