Spaces:
Sleeping

Student3331 commited on
Commit
907d60b
·
verified ·
1 Parent(s): f906461

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -44
app.py CHANGED
@@ -1,59 +1,50 @@
1
  import os
2
- import requests
3
- import nltk
4
  import streamlit as st
 
5
 
6
- # Download necessary NLTK resources
7
- nltk.download('punkt')
8
 
9
- # Set your API key (It's better to store API key securely using environment variables)
10
- GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your-api-key-here") # Replace with your API key or use environment variable
 
 
11
 
12
- # Correct endpoint URL from the API documentation (replace with actual endpoint)
13
- GROQ_API_URL = 'https://api.groq.com/v1/grammar/checktext' # Update with actual endpoint if needed
14
-
15
- # Function to interact with the GROQ API
16
- def check_grammar(text):
17
- headers = {
18
- 'Authorization': f'Bearer {GROQ_API_KEY}',
19
- 'Content-Type': 'application/json',
20
- }
21
-
22
- # Prepare the payload with the text input from the user
23
- payload = {
24
- "text": text
25
- }
26
 
27
- # Send the request to the GROQ API for grammar checking
28
- response = requests.post(GROQ_API_URL, json=payload, headers=headers)
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
- # Streamlit interface for Writing Prompt
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
- feedback = check_grammar(user_input)
49
- st.subheader("Feedback from GROQ API:")
50
- st.write(feedback)
51
  else:
52
  st.write("Please write your paragraph first.")
53
 
54
- # Streamlit interface for Reading Comprehension
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
- feedback = check_grammar(user_answer)
72
- st.subheader("Feedback from GROQ API:")
73
- st.write(feedback)
74
  else:
75
  st.write("Please write your answer first.")
76
 
77
- # Main function to display options and run the interface
78
  def main():
79
- st.title("English Learning Assistant")
80
 
81
- # Display options for the user
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":