#JSON Mode

1 messages · Page 1 of 1 (latest)

drifting smelt
novel marsh
#
model = genai.GenerativeModel('gemini-1.5-flash', generation_config={"response_mime_type": "application/json"})
#

shud be something like this?

drifting smelt
#

I believe so

novel marsh
novel marsh
#
import json

json_data = response.text.strip("\\n")
print(response.text[10:10120])
print(len(response.text))
#json_data = json_data.replace('\\', '\\\\') 
#json_data = json_data.replace('\'', '\"')  
#json_data = json_data.replace('}{', '},{')
json_data = response.text
#del json_data[0:10]
#json_data = json_data.loads()
#print(json_data[3400:3530])  
#print(len(json_data))
#print(json_data[10119])
data = json.loads(json_data[:10117] + ']')
print(data)
data = json.dumps(data)
df_sample = pd.DataFrame(data)
df_sample
#

how can i strip these out?

drifting smelt
#

I'm not sure what needs to be stripped out or why you're adding a ].
You have it set for JSON mode?
Are you streaming or not?

novel marsh
#

@drifting smelt I did like this -

import json

json_data = response.text.strip("\n")
json_data
#json_data = response.text.strip('')
#print(response.text[10:10120])
#print(len(response.text))
#json_data = json_data.replace('\\', '\\\\') 
#json_data = json_data.replace("\\\\n", "\\")

# #json_data = json_data.replace('}{', '},{')
# json_data = response.text
# #del json_data[0:10]
# #json_data = json_data.loads()
#print(json_data[22])  

#print(len(json_data))
#print(json_data[10119])
data = json.loads(json_data)
#print(data)
#data = json.dumps(data)
import numpy as np 
tweets = data['tweets']
#df_sample = pd.read_json(data, orient='index')
df_sample = pd.DataFrame(tweets)
print(df_sample)
#

and it fixed

#
                                               tweet predicted_label
0  ``यह एक शानदार दिन है! मुझे बहुत खुशी हो रही ह...        positive
1                   ``मुझे आज बहुत बुरा लग रहा है।``        negative
2                          ``मैं बस घर जा रहा हूँ।``         neutral
#

but Idk why its not working across all text in the test dataset

drifting smelt
#

how are you getting the model and getting the response?

novel marsh
#
model = genai.GenerativeModel('gemini-1.5-flash', generation_config={"response_mime_type": "application/json"})
#
print(json_data)```
#

let me know if u want any other info

#

@drifting smelt

drifting smelt
#

you don't need to ping me.

#

Have you tried it with a smaller test set? I'm honestly having trouble reading a 10k data structure in a language I don't know. But its supposed to be returning parseable JSON.

novel marsh
novel marsh
#

yah I understand that

novel marsh
#

The error again came back

#
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-49-3c35945d264b> in <cell line: 7>()
      5 #json_data = response.text.strip('')
      6 #print(response.text[10:10120])
----> 7 data = json.loads(json_data)
      8 # import numpy as np
      9 # tweets = data['tweets']

2 frames
/usr/lib/python3.10/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    344             parse_int is None and parse_float is None and
    345             parse_constant is None and object_pairs_hook is None and not kw):
--> 346         return _default_decoder.decode(s)
    347     if cls is None:
    348         cls = JSONDecoder

/usr/lib/python3.10/json/decoder.py in decode(self, s, _w)
    335 
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()
    339         if end != len(s):

/usr/lib/python3.10/json/decoder.py in raw_decode(self, s, idx)
    351         """
    352         try:
--> 353             obj, end = self.scan_once(s, idx)
    354         except StopIteration as err:
    355             raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Invalid \uXXXX escape: line 1 column 8667 (char 8666)
#
import json

json_data = response.text.strip("`").replace("\\\\", "\\")
#json_data = response.text.strip("`")
#json_data = response.text.strip('')
#print(response.text[10:10120])
data = json.loads(json_data)
# import numpy as np
# tweets = data['tweets']
#df_sample = pd.read_json(data, orient='index')
df_sample = pd.DataFrame(tweets)
print(df_sample)