ibrahim313 commited on
Commit
2830be1
·
verified ·
1 Parent(s): 10d6a6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
app.py CHANGED
@@ -1,18 +1,26 @@
1
  import os
2
  import streamlit as st
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
 
5
- # Load the IBM Granite model and tokenizer
6
- model_path = "ibm-granite/granite-8b-code-base" # Replace with the desired model path
7
- tokenizer = AutoTokenizer.from_pretrained(model_path)
8
- model = AutoModelForCausalLM.from_pretrained(model_path, device_map="auto")
9
 
10
- # Function to generate text using the IBM Granite model
11
- def generate_text(prompt):
12
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True, max_length=512)
13
- outputs = model.generate(**inputs, max_new_tokens=150)
14
- generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
15
- return generated_text
 
 
 
 
 
 
 
 
 
 
16
 
17
  # Define functions for each tool
18
  def personalized_learning_assistant(topic):
@@ -22,7 +30,7 @@ def personalized_learning_assistant(topic):
22
  "Explain machine learning. Example: Machine learning involves algorithms that improve through experience."
23
  ]
24
  prompt = f"Here are some examples of explanations:\n\n{examples}\n\nNow, explain the topic: {topic}"
25
- return generate_text(prompt)
26
 
27
  def ai_coding_mentor(code_snippet):
28
  examples = [
@@ -30,7 +38,7 @@ def ai_coding_mentor(code_snippet):
30
  "Review this code:\n\nCode: 'def add(a, b): return a + b'\nSuggestion: Add type hints for better readability."
31
  ]
32
  prompt = f"Here are some examples of code reviews:\n\n{examples}\n\nReview the following code snippet:\n{code_snippet}"
33
- return generate_text(prompt)
34
 
35
  def smart_document_summarizer(document_text):
36
  examples = [
@@ -38,7 +46,7 @@ def smart_document_summarizer(document_text):
38
  "Summarize this passage:\n\nText: 'The global climate change crisis necessitates urgent action to reduce carbon emissions.'\nSummary: 'Immediate action is needed to tackle climate change.'"
39
  ]
40
  prompt = f"Here are some examples of summaries:\n\n{examples}\n\nSummarize this document:\n{document_text}"
41
- return generate_text(prompt)
42
 
43
  def interactive_study_planner(exam_schedule):
44
  examples = [
@@ -46,7 +54,7 @@ def interactive_study_planner(exam_schedule):
46
  "Generate a study plan for:\n\nSchedule: 'Exams in 2 weeks'\nPlan: 'Focus on subjects with more weight and review daily.'"
47
  ]
48
  prompt = f"Here are some examples of study plans:\n\n{examples}\n\nCreate a study plan for the following schedule:\n{exam_schedule}"
49
- return generate_text(prompt)
50
 
51
  def real_time_qa_support(question):
52
  examples = [
@@ -54,7 +62,7 @@ def real_time_qa_support(question):
54
  "Provide an explanation for:\n\nQuestion: 'What is photosynthesis?'\nAnswer: 'Photosynthesis is the process by which plants convert light energy into chemical energy.'"
55
  ]
56
  prompt = f"Here are some examples of Q&A responses:\n\n{examples}\n\nAnswer the following question:\n{question}"
57
- return generate_text(prompt)
58
 
59
  def mental_health_check_in(feelings):
60
  examples = [
@@ -62,7 +70,7 @@ def mental_health_check_in(feelings):
62
  "Offer support for:\n\nFeeling: 'Feeling overwhelmed'\nAdvice: 'Consider relaxation techniques and seek support from friends or a counselor.'"
63
  ]
64
  prompt = f"Here are some examples of mental health advice:\n\n{examples}\n\nProvide advice for:\n{feelings}"
65
- return generate_text(prompt)
66
 
67
  # Define Streamlit app
68
  st.set_page_config(page_title="EduNexus", page_icon=":book:", layout="wide")
 
1
  import os
2
  import streamlit as st
3
+ from groq import Groq
4
 
5
+ # Set the Groq API key
6
+ os.environ["GROQ_API_KEY"] = "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"
 
 
7
 
8
+ # Initialize Groq client
9
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
10
+
11
+ # Define the LLaMA model to be used
12
+ MODEL_NAME = "llama3-8b-8192"
13
+
14
+ # Function to call Groq API
15
+ def call_groq_api(prompt):
16
+ try:
17
+ chat_completion = client.chat.completions.create(
18
+ messages=[{"role": "user", "content": prompt}],
19
+ model=MODEL_NAME
20
+ )
21
+ return chat_completion.choices[0].message.content
22
+ except Exception as e:
23
+ return f"Error: {str(e)}"
24
 
25
  # Define functions for each tool
26
  def personalized_learning_assistant(topic):
 
30
  "Explain machine learning. Example: Machine learning involves algorithms that improve through experience."
31
  ]
32
  prompt = f"Here are some examples of explanations:\n\n{examples}\n\nNow, explain the topic: {topic}"
33
+ return call_groq_api(prompt)
34
 
35
  def ai_coding_mentor(code_snippet):
36
  examples = [
 
38
  "Review this code:\n\nCode: 'def add(a, b): return a + b'\nSuggestion: Add type hints for better readability."
39
  ]
40
  prompt = f"Here are some examples of code reviews:\n\n{examples}\n\nReview the following code snippet:\n{code_snippet}"
41
+ return call_groq_api(prompt)
42
 
43
  def smart_document_summarizer(document_text):
44
  examples = [
 
46
  "Summarize this passage:\n\nText: 'The global climate change crisis necessitates urgent action to reduce carbon emissions.'\nSummary: 'Immediate action is needed to tackle climate change.'"
47
  ]
48
  prompt = f"Here are some examples of summaries:\n\n{examples}\n\nSummarize this document:\n{document_text}"
49
+ return call_groq_api(prompt)
50
 
51
  def interactive_study_planner(exam_schedule):
52
  examples = [
 
54
  "Generate a study plan for:\n\nSchedule: 'Exams in 2 weeks'\nPlan: 'Focus on subjects with more weight and review daily.'"
55
  ]
56
  prompt = f"Here are some examples of study plans:\n\n{examples}\n\nCreate a study plan for the following schedule:\n{exam_schedule}"
57
+ return call_groq_api(prompt)
58
 
59
  def real_time_qa_support(question):
60
  examples = [
 
62
  "Provide an explanation for:\n\nQuestion: 'What is photosynthesis?'\nAnswer: 'Photosynthesis is the process by which plants convert light energy into chemical energy.'"
63
  ]
64
  prompt = f"Here are some examples of Q&A responses:\n\n{examples}\n\nAnswer the following question:\n{question}"
65
+ return call_groq_api(prompt)
66
 
67
  def mental_health_check_in(feelings):
68
  examples = [
 
70
  "Offer support for:\n\nFeeling: 'Feeling overwhelmed'\nAdvice: 'Consider relaxation techniques and seek support from friends or a counselor.'"
71
  ]
72
  prompt = f"Here are some examples of mental health advice:\n\n{examples}\n\nProvide advice for:\n{feelings}"
73
+ return call_groq_api(prompt)
74
 
75
  # Define Streamlit app
76
  st.set_page_config(page_title="EduNexus", page_icon=":book:", layout="wide")