hra commited on
Commit
2f2ab4e
1 Parent(s): a9c9087

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import requests
3
+ import gradio as gr
4
+ import random
5
+ import time
6
+ import os
7
+ import datetime
8
+ from datetime import datetime
9
+ from PIL import Image
10
+ from PIL import ImageOps
11
+ from PIL import Image, ImageDraw, ImageFont
12
+ import json
13
+ import io
14
+ from PIL import Image
15
+
16
+ API_TOKEN = os.getenv("OPENAI_API_TOKEN")
17
+ HRA_TOKEN=os.getenv("HRA_TOKEN")
18
+
19
+ headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
20
+ url_hraprompts='https://us-central1-createinsightsproject.cloudfunctions.net/gethrahfprompts'
21
+
22
+ data={"prompt_type":'mindmap_prompt',"hra_token":HRA_TOKEN}
23
+ try:
24
+ r = requests.post(url_hraprompts, data=json.dumps(data), headers=headers)
25
+ except requests.exceptions.ReadTimeout as e:
26
+ print(e)
27
+ #print(r.content)
28
+
29
+
30
+ prompt_text=str(r.content, 'UTF-8')
31
+ print(prompt_text)
32
+
33
+ def getmindmap(topic,openapikey):
34
+ dateforfilesave=datetime.today().strftime("%d-%m-%Y %I:%M%p")
35
+ print(topic)
36
+ print(dateforfilesave)
37
+
38
+ os.environ['OPENAI_API_KEY'] = str(openapikey)
39
+
40
+ prompt=prompt_text+topic
41
+ resp=openai.Completion.create(
42
+ model="text-davinci-003",
43
+ prompt=prompt,
44
+ max_tokens=4000,
45
+ temperature=0
46
+ )
47
+
48
+
49
+ df=pd.DataFrame(json.loads(resp['choices'][0]['text']))
50
+
51
+ df['level1']=df['children'].apply(lambda x: x['name'])
52
+ df['level1_tmp']=df['children'].apply(lambda x: x['children'])
53
+ s = df.pop('level1_tmp').explode().to_frame()
54
+ df = pd.merge(df.reset_index(),
55
+ s.reset_index(),on='index'
56
+
57
+ )
58
+ df['level2']=df['level1_tmp'].apply(lambda x: x['name'])
59
+ df['count']=[1]*len(df)
60
+
61
+ dot = Digraph()
62
+
63
+ dot.graph_attr['rankdir'] = 'LR'
64
+ for item in list(set(df['level1'].tolist())):
65
+ dot.edge(str(list(set(df["name"].tolist()))[0]), str(item), label='')
66
+ for item in list(set(df['level1'].tolist())):
67
+ tempdf=df[df['level1']==item]
68
+ for stuff in tempdf['level2'].tolist():
69
+ dot.edge(str(item), str(stuff), label='',)
70
+
71
+ r=requests.get('https://quickchart.io/graphviz?format=png&graph='+dot.source)
72
+ dataBytesIO = io.BytesIO(r.content)
73
+ img=Image.open(dataBytesIO)
74
+ img.seek(0)
75
+ name='temp.png'
76
+ img.save(name)
77
+
78
+ return img
79
+
80
+ with gr.Blocks() as demo:
81
+ gr.Markdown("<h1><center>Mind Map Generator</center></h1>")
82
+ gr.Markdown(
83
+ """Enter a topic and get a quick Mind Map. Use examples as a guide. \n\nNote: Error typically occurs with wrong OpenAI API key & sometimes with ChatGPT's inability to give a structured mindmap"""
84
+ )
85
+ with gr.Row() as row:
86
+ with gr.Column():
87
+ textbox1 = gr.Textbox(placeholder="Enter topic for Mind Map...", lines=1,label='Your topic (Mandatory)')
88
+ textbox2 = gr.Textbox(placeholder="Enter OpenAI API Key...", lines=1,label='Your API Key (Mandatory)')
89
+ btn = gr.Button("Generate")
90
+ examples = gr.Examples(examples=['English Premier League','Heavy metal music','Face recognition','Arsenal Football Club',],
91
+ inputs=[textbox])
92
+ with gr.Column():
93
+ output_image1 = gr.components.Image(label="Your Mind Map")
94
+
95
+
96
+ btn.click(getmindmap,inputs=[textbox1,textbox2], outputs=[output_image1,output_image2])
97
+
98
+
99
+ demo.launch()