Student3331 commited on
Commit
d56757b
·
verified ·
1 Parent(s): ffab9f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -22
app.py CHANGED
@@ -1,32 +1,39 @@
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.")
@@ -41,7 +48,7 @@ def writing_prompt():
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
 
@@ -68,7 +75,7 @@ def reading_comprehension():
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":
 
1
  import os
2
+ import requests
3
  import streamlit as st
 
4
 
5
+ # Set the Groq API key from environment variables
6
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY", "your-api-key-here") # Replace with your actual API key if needed
7
 
8
+ # The API URL (update with the correct URL if needed)
9
+ GROQ_API_URL = 'https://api.groq.com/v1/chat/completions' # Update with actual endpoint
 
 
10
 
11
+ # Function to interact with the Groq API
12
  def get_chat_completion(user_message):
13
+ headers = {
14
+ 'Authorization': f'Bearer {GROQ_API_KEY}',
15
+ 'Content-Type': 'application/json',
16
+ }
17
+
18
+ # Prepare the request payload
19
+ payload = {
20
+ "messages": [
21
+ {"role": "user", "content": user_message}
22
  ],
23
+ "model": "llama3-8b-8192", # Replace with the model you're using
24
+ }
25
+
26
+ # Send the request to the API
27
+ response = requests.post(GROQ_API_URL, json=payload, headers=headers)
28
 
29
+ if response.status_code == 200:
30
+ # Return the response content
31
+ return response.json()['choices'][0]['message']['content']
32
+ else:
33
+ # Handle error response
34
+ return f"Error: {response.status_code} - {response.text}"
35
 
36
+ # Streamlit Writing Prompt
37
  def writing_prompt():
38
  st.title("English Writing Assistant - Writing Prompt")
39
  st.write("Write a paragraph about your favorite book.")
 
48
  else:
49
  st.write("Please write your paragraph first.")
50
 
51
+ # Streamlit Reading Comprehension
52
  def reading_comprehension():
53
  st.title("Reading Comprehension Assistant")
54
 
 
75
  def main():
76
  st.title("English Learning Assistant with Groq API")
77
 
78
+ # Choose between activities
79
  option = st.selectbox("Choose an activity", ["Writing Prompt", "Reading Comprehension"])
80
 
81
  if option == "Writing Prompt":