sentence_fix / app.py
taufiqdp's picture
Update app.py
2c36cb2
raw
history blame
2.18 kB
import gradio as gr
import google.generativeai as palm
import os
# Get your own API key on https://developers.generativeai.google/products/palm
api_key = os.environ["api_key"]
palm.configure(api_key=api_key)
defaults = {
'model': 'models/text-bison-001',
'temperature': 0.4,
'candidate_count': 1,
'top_k': 40,
'top_p': 0.95,
'max_output_tokens': 1024,
'stop_sequences': [],
'safety_settings': [
{"category": "HARM_CATEGORY_DEROGATORY", "threshold": 3},
{"category": "HARM_CATEGORY_TOXICITY", "threshold": 3},
{"category": "HARM_CATEGORY_VIOLENCE", "threshold": 3},
{"category": "HARM_CATEGORY_SEXUAL", "threshold": 3},
{"category": "HARM_CATEGORY_MEDICAL", "threshold": 3},
{"category": "HARM_CATEGORY_DANGEROUS", "threshold": 3}]
}
with gr.Blocks() as app:
def chat(text):
try:
response = palm.generate_text(
**defaults,
prompt=f"""Please correct these sentences and rectify any grammar errors. Additionally, when making your corrections, kindly refrain from including quotation marks in your revised sentences. The objective is to enhance the overall clarity and coherence of the paragraph. Do not add any sentences other than the correction of the sentences. If does not appear to have a specific name or meaning in any known language or it appears to be a random string of letters without a clear linguistic structure or purpose give response 'Sentences must be in English'.
Sentences: '{text}'"""
)
return response.result
except:
return "Sentences must be in English"
with gr.Column():
text = gr.Textbox(lines=6, label="Text", max_lines=5, placeholder="Write something awesome. It will be corrected automatically.")
with gr.Column():
output = gr.Textbox(lines=6, label="Output", max_lines=6, show_copy_button=True)
with gr.Row():
clr_btn = gr.ClearButton([text, output])
btn = gr.Button("Submit", variant="primary")
btn.click(fn=chat, inputs=text, outputs=output)
if __name__ == "__main__":
app.launch()