Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install necessary libraries
|
| 2 |
+
!pip install requests nltk streamlit
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import requests
|
| 6 |
+
import nltk
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from nltk.tokenize import word_tokenize, sent_tokenize
|
| 9 |
+
|
| 10 |
+
# Download necessary NLTK resources
|
| 11 |
+
nltk.download('punkt')
|
| 12 |
+
|
| 13 |
+
# Set the environmental variable for GROQ API key
|
| 14 |
+
# Replace 'your_groq_api_key' with your actual API key
|
| 15 |
+
os.environ["GROQ_API_KEY"] = "your_groq_api_key" # Set your key here
|
| 16 |
+
|
| 17 |
+
# Retrieve the API key from the environment variable
|
| 18 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 19 |
+
GROQ_API_URL = 'https://api.groq.com/v1/your_endpoint' # Replace with actual endpoint
|
| 20 |
+
|
| 21 |
+
# Function to interact with GROQ API
|
| 22 |
+
def check_grammar(text):
|
| 23 |
+
headers = {
|
| 24 |
+
'Authorization': f'Bearer {GROQ_API_KEY}',
|
| 25 |
+
'Content-Type': 'application/json',
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# Prepare the payload with the text input from the user
|
| 29 |
+
payload = {
|
| 30 |
+
"text": text
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# Send the request to the GROQ API for grammar checking
|
| 34 |
+
response = requests.post(GROQ_API_URL, json=payload, headers=headers)
|
| 35 |
+
|
| 36 |
+
if response.status_code == 200:
|
| 37 |
+
# Assuming the API returns a JSON response with feedback
|
| 38 |
+
feedback = response.json()
|
| 39 |
+
return feedback
|
| 40 |
+
else:
|
| 41 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 42 |
+
|
| 43 |
+
# Streamlit interface for Writing Prompt
|
| 44 |
+
def writing_prompt():
|
| 45 |
+
st.title("English Writing Assistant")
|
| 46 |
+
st.write("Write a paragraph about your favorite book.")
|
| 47 |
+
|
| 48 |
+
# Input from user
|
| 49 |
+
user_input = st.text_area("Your paragraph", height=200)
|
| 50 |
+
|
| 51 |
+
if st.button("Submit"):
|
| 52 |
+
if user_input:
|
| 53 |
+
feedback = check_grammar(user_input)
|
| 54 |
+
st.subheader("Feedback from GROQ API:")
|
| 55 |
+
st.write(feedback)
|
| 56 |
+
else:
|
| 57 |
+
st.write("Please write your paragraph first.")
|
| 58 |
+
|
| 59 |
+
# Streamlit interface for Reading Comprehension
|
| 60 |
+
def reading_comprehension():
|
| 61 |
+
st.title("Reading Comprehension Assistant")
|
| 62 |
+
passage = """
|
| 63 |
+
English is a global language spoken by millions of people worldwide. It is used for communication, business, and travel. Learning English opens doors to new opportunities, whether in terms of education or career. Many people choose to learn English to improve their chances of success in an increasingly interconnected world.
|
| 64 |
+
"""
|
| 65 |
+
st.write(f"Reading Passage: {passage}")
|
| 66 |
+
st.write("\nNow, answer the following question based on the passage.")
|
| 67 |
+
|
| 68 |
+
question = "Why is learning English important?"
|
| 69 |
+
st.write(f"Question: {question}")
|
| 70 |
+
|
| 71 |
+
# User input for answer
|
| 72 |
+
user_answer = st.text_area("Your answer", height=100)
|
| 73 |
+
|
| 74 |
+
if st.button("Submit Answer"):
|
| 75 |
+
if user_answer:
|
| 76 |
+
feedback = check_grammar(user_answer)
|
| 77 |
+
st.subheader("Feedback from GROQ API:")
|
| 78 |
+
st.write(feedback)
|
| 79 |
+
else:
|
| 80 |
+
st.write("Please write your answer first.")
|
| 81 |
+
|
| 82 |
+
# Main function to display options and run the interface
|
| 83 |
+
def main():
|
| 84 |
+
st.title("English Learning Assistant")
|
| 85 |
+
|
| 86 |
+
# Display options for the user
|
| 87 |
+
option = st.selectbox("Choose an activity", ["Writing Prompt", "Reading Comprehension"])
|
| 88 |
+
|
| 89 |
+
if option == "Writing Prompt":
|
| 90 |
+
writing_prompt()
|
| 91 |
+
elif option == "Reading Comprehension":
|
| 92 |
+
reading_comprehension()
|
| 93 |
+
|
| 94 |
+
if __name__ == '__main__':
|
| 95 |
+
main()
|