Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,50 @@
|
|
| 1 |
import os
|
| 2 |
-
import requests
|
| 3 |
-
import nltk
|
| 4 |
import streamlit as st
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
"text": text
|
| 25 |
-
}
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
if response.status_code == 200:
|
| 31 |
-
# Assuming the API returns a JSON response with feedback
|
| 32 |
-
feedback = response.json()
|
| 33 |
-
return feedback
|
| 34 |
-
else:
|
| 35 |
-
# Log the error for debugging purposes
|
| 36 |
-
return f"Error: {response.status_code} - {response.text}"
|
| 37 |
|
| 38 |
-
#
|
| 39 |
def writing_prompt():
|
| 40 |
-
st.title("English Writing Assistant")
|
| 41 |
st.write("Write a paragraph about your favorite book.")
|
| 42 |
|
| 43 |
-
# Input from user
|
| 44 |
user_input = st.text_area("Your paragraph", height=200)
|
| 45 |
|
| 46 |
if st.button("Submit"):
|
| 47 |
if user_input:
|
| 48 |
-
|
| 49 |
-
st.subheader("Feedback from
|
| 50 |
-
st.write(
|
| 51 |
else:
|
| 52 |
st.write("Please write your paragraph first.")
|
| 53 |
|
| 54 |
-
#
|
| 55 |
def reading_comprehension():
|
| 56 |
st.title("Reading Comprehension Assistant")
|
|
|
|
| 57 |
passage = """
|
| 58 |
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.
|
| 59 |
"""
|
|
@@ -63,22 +54,21 @@ def reading_comprehension():
|
|
| 63 |
question = "Why is learning English important?"
|
| 64 |
st.write(f"Question: {question}")
|
| 65 |
|
| 66 |
-
# User input for answer
|
| 67 |
user_answer = st.text_area("Your answer", height=100)
|
| 68 |
|
| 69 |
if st.button("Submit Answer"):
|
| 70 |
if user_answer:
|
| 71 |
-
|
| 72 |
-
st.subheader("Feedback from
|
| 73 |
-
st.write(
|
| 74 |
else:
|
| 75 |
st.write("Please write your answer first.")
|
| 76 |
|
| 77 |
-
# Main function to display
|
| 78 |
def main():
|
| 79 |
-
st.title("English Learning Assistant")
|
| 80 |
|
| 81 |
-
#
|
| 82 |
option = st.selectbox("Choose an activity", ["Writing Prompt", "Reading Comprehension"])
|
| 83 |
|
| 84 |
if option == "Writing Prompt":
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
from groq import Groq
|
| 4 |
|
| 5 |
+
# Streamlit page config
|
| 6 |
+
st.set_page_config(page_title="English Writing Assistant", layout="centered")
|
| 7 |
|
| 8 |
+
# Initialize Groq client with the API key stored in environment variables
|
| 9 |
+
client = Groq(
|
| 10 |
+
api_key=os.environ.get("GROQ_API_KEY") # Use the environment variable for the API key
|
| 11 |
+
)
|
| 12 |
|
| 13 |
+
# Function to handle user input and get response from the Groq API
|
| 14 |
+
def get_chat_completion(user_message):
|
| 15 |
+
# Call the Groq chat completion endpoint
|
| 16 |
+
chat_completion = client.chat.completions.create(
|
| 17 |
+
messages=[
|
| 18 |
+
{
|
| 19 |
+
"role": "user",
|
| 20 |
+
"content": user_message,
|
| 21 |
+
}
|
| 22 |
+
],
|
| 23 |
+
model="llama3-8b-8192", # Replace with the desired model if necessary
|
| 24 |
+
)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Extract and return the response message content
|
| 27 |
+
return chat_completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Function to handle the Writing Prompt interaction
|
| 30 |
def writing_prompt():
|
| 31 |
+
st.title("English Writing Assistant - Writing Prompt")
|
| 32 |
st.write("Write a paragraph about your favorite book.")
|
| 33 |
|
|
|
|
| 34 |
user_input = st.text_area("Your paragraph", height=200)
|
| 35 |
|
| 36 |
if st.button("Submit"):
|
| 37 |
if user_input:
|
| 38 |
+
response = get_chat_completion(f"Provide feedback on the following paragraph: {user_input}")
|
| 39 |
+
st.subheader("Feedback from Groq API:")
|
| 40 |
+
st.write(response)
|
| 41 |
else:
|
| 42 |
st.write("Please write your paragraph first.")
|
| 43 |
|
| 44 |
+
# Function to handle the Reading Comprehension interaction
|
| 45 |
def reading_comprehension():
|
| 46 |
st.title("Reading Comprehension Assistant")
|
| 47 |
+
|
| 48 |
passage = """
|
| 49 |
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.
|
| 50 |
"""
|
|
|
|
| 54 |
question = "Why is learning English important?"
|
| 55 |
st.write(f"Question: {question}")
|
| 56 |
|
|
|
|
| 57 |
user_answer = st.text_area("Your answer", height=100)
|
| 58 |
|
| 59 |
if st.button("Submit Answer"):
|
| 60 |
if user_answer:
|
| 61 |
+
response = get_chat_completion(f"Provide feedback on the following answer to the question '{question}': {user_answer}")
|
| 62 |
+
st.subheader("Feedback from Groq API:")
|
| 63 |
+
st.write(response)
|
| 64 |
else:
|
| 65 |
st.write("Please write your answer first.")
|
| 66 |
|
| 67 |
+
# Main function to display the interface
|
| 68 |
def main():
|
| 69 |
+
st.title("English Learning Assistant with Groq API")
|
| 70 |
|
| 71 |
+
# Select between Writing Prompt and Reading Comprehension
|
| 72 |
option = st.selectbox("Choose an activity", ["Writing Prompt", "Reading Comprehension"])
|
| 73 |
|
| 74 |
if option == "Writing Prompt":
|