Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
import os
|
8 |
+
from collections import deque
|
9 |
+
from typing import Dict, List, Optional, Any
|
10 |
+
|
11 |
+
from langchain import LLMChain, OpenAI, PromptTemplate
|
12 |
+
from langchain.embeddings import OpenAIEmbeddings
|
13 |
+
from langchain.llms import BaseLLM
|
14 |
+
from langchain.vectorstores.base import VectorStore
|
15 |
+
from pydantic import BaseModel, Field
|
16 |
+
from langchain.chains.base import Chain
|
17 |
+
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
|
18 |
+
from langchain import OpenAI, LLMChain
|
19 |
+
from langchain.chat_models import ChatOpenAI
|
20 |
+
import datetime
|
21 |
+
from datetime import datetime, date, time, timedelta
|
22 |
+
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader, LLMPredictor, Document, ServiceContext
|
23 |
+
from langchain.llms import OpenAIChat
|
24 |
+
|
25 |
+
import feedparser
|
26 |
+
import pandas as pd
|
27 |
+
import numpy as np
|
28 |
+
|
29 |
+
from duckduckgo_search import ddg_videos
|
30 |
+
from duckduckgo_search import ddg
|
31 |
+
|
32 |
+
def get_learning_curriculum(openapikey,topic):
|
33 |
+
dateforfilesave=datetime.today().strftime("%d-%m-%Y %I:%M%p")
|
34 |
+
print(topic)
|
35 |
+
print(dateforfilesave)
|
36 |
+
if openapikey=='':
|
37 |
+
return pd.DataFrame(["Please provide OpenAPI Key"],columns=['ERROR'])
|
38 |
+
|
39 |
+
os.environ['OPENAI_API_KEY'] = str(openapikey)
|
40 |
+
|
41 |
+
prompt='You are a training center AI. Give me a detailed curriculum to learn about "{topicforquery}" using search. The curriculum will be a series of learning tasks to be achieved. Give output as a python list of jsons with "task name", "search keyword" to search to complete the task. Donot repeat the taks. For each task name also add a list of "questions" to ask the search results data to select specific articles and complete the curriculum. Remember the search list will be a dataframe of titles & body of the searched article and you may not be able to go through the full article hence these questions should be of types "Which article best suits a learning curriculum?", "Which article is learning oriented?. To reiterate output should be in json with keys task name ex: get beginner training articles for painting, search keyword ex: beginner painting & questions ex: What are top articles for painting?'.format(topicforquery=topic)
|
42 |
+
|
43 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
44 |
+
resp=openai.ChatCompletion.create(
|
45 |
+
model="gpt-3.5-turbo",
|
46 |
+
messages=[
|
47 |
+
{"role": "user", "content": prompt}
|
48 |
+
]
|
49 |
+
)
|
50 |
+
tasklist=json.loads(resp['choices'][0]['message']['content'])
|
51 |
+
|
52 |
+
llm = ChatOpenAI(temperature=0)
|
53 |
+
|
54 |
+
def research_search(search_keyword,question_to_ask,topic):
|
55 |
+
llm_predictor = LLMPredictor(llm=OpenAIChat(temperature=0, model_name="gpt-3.5-turbo"))
|
56 |
+
service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor)
|
57 |
+
keyword=search_keyword
|
58 |
+
keyword="+".join(keyword.lower().split())
|
59 |
+
keyword=keyword.replace(' and ',' AND ')
|
60 |
+
posts = ddg(keyword+' '+topic, safesearch='Off', page=1)
|
61 |
+
latestnews_df=pd.DataFrame(posts)
|
62 |
+
print(latestnews_df.columns)
|
63 |
+
#latestnews_df=latestnews_df.drop_duplicates(subset=['title','link','published'])
|
64 |
+
latestnews_df['text']='Title: '+latestnews_df['title']+' Description: '+latestnews_df['body']
|
65 |
+
print(latestnews_df['text'].tolist())
|
66 |
+
documents=[Document(t) for t in latestnews_df['text'].tolist()]
|
67 |
+
index = GPTSimpleVectorIndex.from_documents(documents)
|
68 |
+
prompt_query=question_to_ask
|
69 |
+
respstr=str(index.query(prompt_query,
|
70 |
+
service_context=service_context,
|
71 |
+
response_mode="tree_summarize",
|
72 |
+
similarity_top_k=10))
|
73 |
+
print("Search response: ",respstr)
|
74 |
+
return respstr
|
75 |
+
|
76 |
+
finallist=[]
|
77 |
+
list1=[]
|
78 |
+
list2=[]
|
79 |
+
list3=[]
|
80 |
+
for i in range(len(tasklist)):
|
81 |
+
taskstuff=tasklist[i]
|
82 |
+
search_keyword=taskstuff['search keyword']
|
83 |
+
print('Task Name: '+taskstuff['task name'])
|
84 |
+
finallist.append('Task Name: '+taskstuff['task name'])
|
85 |
+
|
86 |
+
for question in taskstuff['questions']:
|
87 |
+
response_string=research_search(search_keyword,question,topic)
|
88 |
+
finallist.append(" Question: "+question)
|
89 |
+
finallist.append(" "+response_string)
|
90 |
+
|
91 |
+
list1.append(taskstuff['task name'])
|
92 |
+
list2.append(question)
|
93 |
+
list3.append(response_string)
|
94 |
+
|
95 |
+
outputdf=pd.DataFrame()
|
96 |
+
outputdf['Task']=list1
|
97 |
+
outputdf['Question']=list2
|
98 |
+
outputdf['Learning']=list3
|
99 |
+
|
100 |
+
return outputdf
|
101 |
+
|
102 |
+
with gr.Blocks() as demo:
|
103 |
+
gr.Markdown("<h1><center>BabyAGI creates Learning Curriculum</center></h1>")
|
104 |
+
gr.Markdown(
|
105 |
+
"""What are the sectors with positive momentum? What are the macro trends? Which companies have momentum? Sector summaries and much more. \n\nThis is a demo & showcases ChatGPT integrated with real data. It shows how to get real-time data and marry it with ChatGPT capabilities. This demonstrates 'Chain of Thought' thinking using ChatGPT.\n\n4 snapshots are provided for illustration (trends, sector outlook, news summary email, macro trends email)\n\nNote: llama-index & gpt-3.5-turbo are used. The analysis takes roughly 120 secs & may not always be consistent. If ChatGPT API is overloaded you will get an error\n ![visitors](https://visitor-badge.glitch.me/badge?page_id=hra.chatgpt-stock-news-snapshots)"""
|
106 |
+
)
|
107 |
+
|
108 |
+
with gr.Row() as row:
|
109 |
+
with gr.Column():
|
110 |
+
textboxtopic = gr.Textbox(placeholder="Enter Topic for Curriculum...", lines=1,label='Topic')
|
111 |
+
with gr.Column():
|
112 |
+
textboxopenapi = gr.Textbox(placeholder="Enter OpenAPI Key...", lines=1,label='OpenAPI Key')
|
113 |
+
|
114 |
+
with gr.Row() as row:
|
115 |
+
btn = gr.Button("Generate \nCurriculum")
|
116 |
+
|
117 |
+
with gr.Row() as row:
|
118 |
+
table1=gr.Dataframe(
|
119 |
+
#headers=["Item", "Cost"],
|
120 |
+
#datatype=["str", "str","str"],
|
121 |
+
label="Learning Curriculum",
|
122 |
+
)
|
123 |
+
|
124 |
+
|
125 |
+
btn.click(getstuff, inputs=[textboxopenapi,textboxtopic],outputs=[table1])
|
126 |
+
|
127 |
+
|
128 |
+
demo.launch(debug=True)
|