Update app.py
Browse files
app.py
CHANGED
@@ -17,6 +17,84 @@ sentence_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
|
17 |
# Initialize Groq client
|
18 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
# Function to get BERT embeddings
|
21 |
def get_bert_embedding(text):
|
22 |
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True)
|
|
|
17 |
# Initialize Groq client
|
18 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
19 |
|
20 |
+
|
21 |
+
# System prompt for Groq
|
22 |
+
system_prompt = {
|
23 |
+
"role": "system",
|
24 |
+
"content": "You are a useful assistant. You reply with efficient answers."
|
25 |
+
}
|
26 |
+
|
27 |
+
# Function to interact with Groq for generating response
|
28 |
+
async def chat_groq(message, history):
|
29 |
+
messages = [system_prompt]
|
30 |
+
|
31 |
+
for msg in history:
|
32 |
+
messages.append({"role": "user", "content": str(msg[0])})
|
33 |
+
messages.append({"role": "assistant", "content": str(msg[1])})
|
34 |
+
|
35 |
+
messages.append({"role": "user", "content": str(message)})
|
36 |
+
|
37 |
+
response_content = ''
|
38 |
+
|
39 |
+
stream = client.chat.completions.create(
|
40 |
+
model="llama3-70b-8192",
|
41 |
+
messages=messages,
|
42 |
+
max_tokens=1024,
|
43 |
+
temperature=1.3,
|
44 |
+
stream=True
|
45 |
+
)
|
46 |
+
|
47 |
+
for chunk in stream:
|
48 |
+
content = chunk.choices[0].delta.content
|
49 |
+
if content:
|
50 |
+
response_content += chunk.choices[0].delta.content
|
51 |
+
yield response_content
|
52 |
+
|
53 |
+
# Extract text from an image using Tesseract
|
54 |
+
def extract_text_from_image(filepath: str, languages: List[str]):
|
55 |
+
image = Image.open(filepath)
|
56 |
+
lang_str = '+'.join(languages) # Join languages for Tesseract
|
57 |
+
return pytesseract.image_to_string(image=image, lang=lang_str)
|
58 |
+
|
59 |
+
|
60 |
+
# Assign badges based on the grade
|
61 |
+
def assign_badge(grade):
|
62 |
+
if grade == 5:
|
63 |
+
return "Gold Badge 🌟"
|
64 |
+
elif grade == 4:
|
65 |
+
return "Silver Badge 🥈"
|
66 |
+
elif grade == 3:
|
67 |
+
return "Bronze Badge 🥉"
|
68 |
+
else:
|
69 |
+
return "Keep Improving Badge 💪"
|
70 |
+
|
71 |
+
|
72 |
+
# Categorize feedback into clarity, completeness, and accuracy
|
73 |
+
def detailed_feedback(similarity_score):
|
74 |
+
if similarity_score >= 0.9:
|
75 |
+
return {"Clarity": "Excellent", "Completeness": "Complete", "Accuracy": "Accurate"}
|
76 |
+
elif similarity_score >= 0.8:
|
77 |
+
return {"Clarity": "Good", "Completeness": "Almost Complete", "Accuracy": "Mostly Accurate"}
|
78 |
+
elif similarity_score >= 0.7:
|
79 |
+
return {"Clarity": "Fair", "Completeness": "Partial", "Accuracy": "Some Errors"}
|
80 |
+
else:
|
81 |
+
return {"Clarity": "Needs Improvement", "Completeness": "Incomplete", "Accuracy": "Inaccurate"}
|
82 |
+
|
83 |
+
# Assign grades based on similarity score
|
84 |
+
def get_grade(similarity_score):
|
85 |
+
if similarity_score >= 0.9:
|
86 |
+
return 5
|
87 |
+
elif similarity_score >= 0.8:
|
88 |
+
return 4
|
89 |
+
elif similarity_score >= 0.7:
|
90 |
+
return 3
|
91 |
+
elif similarity_score >= 0.6:
|
92 |
+
return 2
|
93 |
+
else:
|
94 |
+
return 1
|
95 |
+
|
96 |
+
|
97 |
+
|
98 |
# Function to get BERT embeddings
|
99 |
def get_bert_embedding(text):
|
100 |
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True)
|