Spaces:
Sleeping
Sleeping
Download app.py from Student3331/bot: direct link, hf CLI and curl.
- Browser
- Download file 3.56 kB
-
https://huggingface.co/spaces/Student3331/bot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Student3331/bot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Student3331/bot/resolve/main/app.py
3.56 kB
| 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() | |