Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
from groq import Groq | |
os.environ["GROQ_API_KEY"] = "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" | |
# Set your Groq API key here (for local testing or direct assignment) | |
# Uncomment the following line and replace with your actual Groq API key | |
# os.environ["GROQ_API_KEY"] = "your_groq_api_key_here" | |
# Initialize the Groq client | |
def initialize_client(): | |
api_key = os.getenv("GROQ_API_KEY") | |
if not api_key: | |
raise ValueError("API Key not found. Please set GROQ_API_KEY in the environment variables.") | |
return Groq(api_key=api_key) | |
# Function to check grammar and suggest vocabulary | |
def grammar_and_vocab_checker(client, input_text): | |
""" | |
Uses Groq API to correct grammar and suggest vocabulary improvements. | |
""" | |
prompt = ( | |
f"Correct any grammar errors and suggest better vocabulary for the following text:\n" | |
f"{input_text}\n" | |
f"Response:" | |
) | |
try: | |
chat_completion = client.chat.completions.create( | |
messages=[ | |
{ | |
"role": "user", | |
"content": prompt, | |
} | |
], | |
model="llama3-8b-8192", | |
) | |
return chat_completion.choices[0].message.content | |
except Exception as e: | |
raise RuntimeError(f"An error occurred while communicating with the Groq API: {e}") | |
# Streamlit app interface | |
def main(): | |
st.title("English Grammar and Vocabulary Checker") | |
st.write("Enter your text below to get grammar corrections and vocabulary suggestions.") | |
# Input text box | |
user_input = st.text_area("Your Text", placeholder="Type your text here...") | |
if st.button("Check"): | |
if user_input.strip(): | |
st.info("Processing your input, please wait...") | |
try: | |
# Initialize client and process input | |
client = initialize_client() | |
response = grammar_and_vocab_checker(client, user_input) | |
st.success("Here is the suggestion:") | |
st.write(response) | |
except Exception as e: | |
st.error(str(e)) | |
else: | |
st.warning("Please enter some text to check.") | |
if __name__ == "__main__": | |
main() | |