File size: 3,562 Bytes
e94a6b9
d56757b
e94a6b9
7fc8aa2
 
 
 
e94a6b9
008770d
7fc8aa2
 
 
 
e94a6b9
008770d
 
e94a6b9
008770d
907d60b
d56757b
 
 
 
008770d
 
d56757b
626f69a
008770d
d56757b
 
008770d
d56757b
008770d
d56757b
008770d
626f69a
d56757b
 
 
e94a6b9
d56757b
e94a6b9
907d60b
e94a6b9
 
 
 
 
 
907d60b
 
 
e94a6b9
 
 
d56757b
e94a6b9
 
907d60b
e94a6b9
 
 
 
 
 
 
 
 
 
 
 
 
907d60b
 
 
e94a6b9
 
 
907d60b
e94a6b9
907d60b
e94a6b9
d56757b
e94a6b9
 
 
 
 
 
 
 
2ce3412
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import requests
import streamlit as st
from dotenv import load_dotenv  # Import to load environment variables from .env file

# Load environment variables from a .env file (if you have one)
load_dotenv()

# Fetch the Groq API key from environment variable
GROQ_API_KEY = os.getenv("GROQ_API_KEY")  # Make sure this matches the key in your .env or environment

if not GROQ_API_KEY:
    st.error("API Key not found! Please set your GROQ_API_KEY in the environment.")

# Correct API URL for Groq Chat Completions
GROQ_API_URL = 'https://api.groq.com/openai/v1/chat/completions'  # Correct Groq endpoint

# Function to interact with the Groq API
def get_chat_completion(user_message):
    headers = {
        'Authorization': f'Bearer {GROQ_API_KEY}',
        'Content-Type': 'application/json',
    }

    # Prepare the payload (adjust this based on the Groq API docs)
    payload = {
        "messages": [{"role": "user", "content": user_message}],
        "model": "gpt-4",  # Use the correct model ID that Groq supports (adjust this based on Groq API docs)
    }
    
    # Send the request to the API
    response = requests.post(GROQ_API_URL, json=payload, headers=headers)

    if response.status_code == 200:
        # Successfully received response from Groq API
        return response.json().get('choices', [{}])[0].get('message', {}).get('content', "No response.")
    else:
        # Handle error response
        return f"Error: {response.status_code} - {response.text}"

# Streamlit Writing Prompt
def writing_prompt():
    st.title("English Writing Assistant - Writing Prompt")
    st.write("Write a paragraph about your favorite book.")
    
    user_input = st.text_area("Your paragraph", height=200)
    
    if st.button("Submit"):
        if user_input:
            response = get_chat_completion(f"Provide feedback on the following paragraph: {user_input}")
            st.subheader("Feedback from Groq API:")
            st.write(response)
        else:
            st.write("Please write your paragraph first.")

# Streamlit Reading Comprehension
def reading_comprehension():
    st.title("Reading Comprehension Assistant")
    
    passage = """
    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.
    """
    st.write(f"Reading Passage: {passage}")
    st.write("\nNow, answer the following question based on the passage.")
    
    question = "Why is learning English important?"
    st.write(f"Question: {question}")
    
    user_answer = st.text_area("Your answer", height=100)
    
    if st.button("Submit Answer"):
        if user_answer:
            response = get_chat_completion(f"Provide feedback on the following answer to the question '{question}': {user_answer}")
            st.subheader("Feedback from Groq API:")
            st.write(response)
        else:
            st.write("Please write your answer first.")

# Main function to display the interface
def main():
    st.title("English Learning Assistant with Groq API")
    
    # Choose between activities
    option = st.selectbox("Choose an activity", ["Writing Prompt", "Reading Comprehension"])
    
    if option == "Writing Prompt":
        writing_prompt()
    elif option == "Reading Comprehension":
        reading_comprehension()

if __name__ == '__main__':
    main()