ofermend commited on
Commit
b2d6bf9
·
1 Parent(s): ce90730

added utils

Browse files
Files changed (1) hide show
  1. utils.py +77 -0
utils.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import json
4
+ import re
5
+
6
+ import streamlit as st
7
+
8
+ from langdetect import detect_langs
9
+ from langcodes import Language
10
+
11
+ headers = {
12
+ 'Content-Type': 'application/json',
13
+ 'Accept': '*/*'
14
+ }
15
+
16
+ def identify_language(response):
17
+ lang_code = detect_langs(response)[0].lang
18
+ return Language.make(language=lang_code).display_name()
19
+
20
+ def thumbs_feedback(feedback, **kwargs):
21
+ """
22
+ Sends feedback to Amplitude Analytics
23
+ """
24
+ send_amplitude_data(
25
+ user_query=kwargs.get("user_query", "No user input"),
26
+ bot_response=kwargs.get("bot_response", "No bot response"),
27
+ demo_name=kwargs.get("demo_name", "Unknown"),
28
+ feedback=feedback['score'],
29
+ )
30
+ st.session_state.feedback_key += 1
31
+
32
+ def send_amplitude_data(user_query, bot_response, demo_name, feedback=None):
33
+ # Send query and response to Amplitude Analytics
34
+ amplitude_token = os.environ.get('AMPLITUDE_TOKEN', None)
35
+ if amplitude_token is None:
36
+ return
37
+ data = {
38
+ "api_key": amplitude_token,
39
+ "events": [{
40
+ "device_id": st.session_state.device_id,
41
+ "event_type": "submitted_query",
42
+ "event_properties": {
43
+ "Space Name": demo_name,
44
+ "Demo Type": "Agent",
45
+ "query": user_query,
46
+ "response": bot_response,
47
+ "Response Language": identify_language(bot_response)
48
+ }
49
+ }]
50
+ }
51
+ if feedback:
52
+ data["events"][0]["event_properties"]["feedback"] = feedback
53
+
54
+ response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
55
+ if response.status_code != 200:
56
+ print(f"Amplitude request failed with status code {response.status_code}. Response Text: {response.text}")
57
+
58
+ def escape_dollars_outside_latex(text):
59
+ # Define a regex pattern to find LaTeX equations (double $$ only)
60
+ pattern = r'\$\$.*?\$\$'
61
+ latex_matches = re.findall(pattern, text, re.DOTALL)
62
+
63
+ # Placeholder to temporarily store LaTeX equations
64
+ placeholders = {}
65
+ for i, match in enumerate(latex_matches):
66
+ placeholder = f'__LATEX_PLACEHOLDER_{i}__'
67
+ placeholders[placeholder] = match
68
+ text = text.replace(match, placeholder)
69
+
70
+ # Escape dollar signs in the rest of the text
71
+ text = text.replace('$', '\\$')
72
+
73
+ # Replace placeholders with the original LaTeX equations
74
+ for placeholder, original in placeholders.items():
75
+ text = text.replace(placeholder, original)
76
+ return text
77
+