Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import os | |
api_key = os.getenv("openai_api_key") | |
organization='org-bFx0b9nX8ik0FHAoj7pZufNP' | |
openai.api_key = api_key | |
openai.organization = organization #os.getenv("ORG") | |
system_message = "A model that takes sentence in English which may contain grammatical errors, and responds with 1-the corrected version of the English sentence in the first line and then, 2-for each error -> correction, a concise explanation in Persian language. if there are no errors you respond with 'No Errors'" | |
model_name = 'ft:gpt-3.5-turbo-1106:infercia::8n84ogUY' | |
print(api_key) | |
def check_grammar(text): | |
response = 'Error Communicating' | |
try: | |
response = openai.ChatCompletion.create( | |
model=model_name, | |
messages=[{ | |
"role": "system", | |
"content": system_message, | |
}, | |
{ | |
"role": "user", | |
"content": text, | |
}], | |
) | |
return response.choices[0].message.content | |
except Exception as e: | |
print('Error Happened Here!') | |
return str(e) | |
# Custom CSS for RTL text output | |
rtl_css = """ | |
<style> | |
textarea { | |
direction: rtl !important; | |
} | |
</style> | |
""" | |
# Gradio interface | |
iface = gr.Interface( | |
fn=check_grammar, | |
inputs="text", | |
outputs=gr.Textbox(rtl=True), | |
title="Grammar Checker", | |
description="Enter text to check for grammar errors using OpenAI.", | |
css=rtl_css, | |
) | |
if __name__ == "__main__": | |
iface.launch(share=True) | |