Spaces:
Runtime error
Runtime error
deploy at 2024-08-21 10:24:01.130174
Browse files- config.ini +5 -0
- main.py +48 -11
- timeline.csv +15 -20
config.ini
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[DEFAULT]
|
| 2 |
+
dataset_id = space-backup
|
| 3 |
+
db_dir = data
|
| 4 |
+
private_backup = True
|
| 5 |
+
|
main.py
CHANGED
|
@@ -4,6 +4,7 @@ import os
|
|
| 4 |
import json
|
| 5 |
import pandas as pd
|
| 6 |
import traceback
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
from typing import Literal
|
| 9 |
from pydantic_core import from_json
|
|
@@ -16,6 +17,7 @@ from langchain_openai import ChatOpenAI
|
|
| 16 |
from langchain_anthropic import ChatAnthropic
|
| 17 |
from pydantic import BaseModel, Field, ValidationError
|
| 18 |
from langchain_openai import ChatOpenAI
|
|
|
|
| 19 |
from fasthtml.common import *
|
| 20 |
from fasthtml.components import Svg
|
| 21 |
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
|
@@ -36,7 +38,7 @@ class Event(BaseModel):
|
|
| 36 |
sentiment: Literal["Positive", "Negative"] = Field(..., description="Categorization of the event sentiment")
|
| 37 |
|
| 38 |
class EventResponse(BaseModel):
|
| 39 |
-
events: List[Event] = Field(min_length=10, max_length=
|
| 40 |
|
| 41 |
# Set up the Pydantic output parser
|
| 42 |
parser = PydanticOutputParser(pydantic_object=EventResponse)
|
|
@@ -44,7 +46,7 @@ parser = PydanticOutputParser(pydantic_object=EventResponse)
|
|
| 44 |
# LangChain prompt template with format instructions
|
| 45 |
event_extraction_template = """
|
| 46 |
Extract the time based informations or events from the context and return a list of events with time, event description and event sentiment type whether it was positive or negative event.
|
| 47 |
-
The context may contain information about people, organization or any other entity.
|
| 48 |
|
| 49 |
<context>
|
| 50 |
{context}
|
|
@@ -56,6 +58,8 @@ The response must follow the following schema strictly. There will be penalty fo
|
|
| 56 |
{format_instructions}
|
| 57 |
</schema>
|
| 58 |
|
|
|
|
|
|
|
| 59 |
Output:
|
| 60 |
"""
|
| 61 |
|
|
@@ -67,14 +71,23 @@ event_prompt = PromptTemplate(
|
|
| 67 |
|
| 68 |
# Function to get the appropriate language model based on user selection
|
| 69 |
def getModel(model, key):
|
| 70 |
-
if(model == 'OpenAI'):
|
| 71 |
os.environ['OPENAI_API_KEY'] = key
|
| 72 |
return ChatOpenAI(temperature=0, # Set to 0 for deterministic output
|
| 73 |
model="gpt-4o-2024-08-06", # Using the GPT-4 Turbo model
|
| 74 |
max_tokens=8000) # Limit the response length
|
| 75 |
-
|
| 76 |
os.environ['ANTHROPIC_API_KEY'] = key
|
| 77 |
return ChatAnthropic(model='claude-3-5-sonnet-20240620') # Limit the response length
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
# Function to generate an HTML table from the summary object
|
|
@@ -96,11 +109,19 @@ def generate_timeline_html(timeline):
|
|
| 96 |
for idx, tline in timeline.iterrows():
|
| 97 |
if idx % 2 == 0:
|
| 98 |
rows.append(Li(Div(File("./assets/circle.svg"), cls = "timeline-middle"),
|
| 99 |
-
Div(Time(tline['TimeStr'],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
Hr()))
|
| 101 |
else:
|
| 102 |
rows.append(Li(Div(File("./assets/circle.svg"), cls = "timeline-middle"),
|
| 103 |
-
Div(Time(tline['TimeStr'],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
Hr()))
|
| 105 |
|
| 106 |
return Ul(*rows, cls="timeline timeline-vertical")
|
|
@@ -139,16 +160,26 @@ def get_timeline_df(result):
|
|
| 139 |
return df
|
| 140 |
|
| 141 |
# Placeholder function for Q&A generation
|
| 142 |
-
def generate_timeline(topic, llm):
|
| 143 |
# This function will be implemented later
|
| 144 |
# For now, return a sample DataFrame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
|
| 147 |
wiki_content = wikipedia.run(topic)
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
chain = event_prompt | llm | parser
|
| 150 |
|
| 151 |
-
result = chain.invoke({"context" : wiki_content
|
|
|
|
|
|
|
| 152 |
|
| 153 |
try:
|
| 154 |
# Parse the output using PydanticOutputParser
|
|
@@ -178,7 +209,7 @@ def getConfigForm():
|
|
| 178 |
),
|
| 179 |
Div(
|
| 180 |
Span(Strong('Model: '), cls ="badge"),
|
| 181 |
-
Select(Option("OpenAI"), Option("Anthropic"), id="model", cls = 'select w-full max-w-xs')
|
| 182 |
),
|
| 183 |
Div(
|
| 184 |
Span(Strong('Topic for timeline (Person/Organization/Event): '), cls ="badge"),
|
|
@@ -191,6 +222,10 @@ def getConfigForm():
|
|
| 191 |
cls = "input w-full max-w-xs",
|
| 192 |
placeholder = "Type here")
|
| 193 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
Div(
|
| 195 |
Button("Generate Timeline", cls = 'btn')
|
| 196 |
),
|
|
@@ -231,7 +266,9 @@ async def post(d:dict):
|
|
| 231 |
model = getModel(d['model'], d['secret'])
|
| 232 |
|
| 233 |
# Perform one-pass summarization
|
| 234 |
-
timeline_df = generate_timeline(d['topic'],
|
|
|
|
|
|
|
| 235 |
#qas = pd.read_csv("results_tesla.csv")
|
| 236 |
|
| 237 |
timeline_df.head(10)
|
|
|
|
| 4 |
import json
|
| 5 |
import pandas as pd
|
| 6 |
import traceback
|
| 7 |
+
import wikipedia
|
| 8 |
from datetime import datetime
|
| 9 |
from typing import Literal
|
| 10 |
from pydantic_core import from_json
|
|
|
|
| 17 |
from langchain_anthropic import ChatAnthropic
|
| 18 |
from pydantic import BaseModel, Field, ValidationError
|
| 19 |
from langchain_openai import ChatOpenAI
|
| 20 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 21 |
from fasthtml.common import *
|
| 22 |
from fasthtml.components import Svg
|
| 23 |
from langchain_community.utilities.wikipedia import WikipediaAPIWrapper
|
|
|
|
| 38 |
sentiment: Literal["Positive", "Negative"] = Field(..., description="Categorization of the event sentiment")
|
| 39 |
|
| 40 |
class EventResponse(BaseModel):
|
| 41 |
+
events: List[Event] = Field(min_length=10, max_length=20, description="List of events extracted from the context")
|
| 42 |
|
| 43 |
# Set up the Pydantic output parser
|
| 44 |
parser = PydanticOutputParser(pydantic_object=EventResponse)
|
|
|
|
| 46 |
# LangChain prompt template with format instructions
|
| 47 |
event_extraction_template = """
|
| 48 |
Extract the time based informations or events from the context and return a list of events with time, event description and event sentiment type whether it was positive or negative event.
|
| 49 |
+
The context may contain information about people, organization or any other entity.
|
| 50 |
|
| 51 |
<context>
|
| 52 |
{context}
|
|
|
|
| 58 |
{format_instructions}
|
| 59 |
</schema>
|
| 60 |
|
| 61 |
+
Must ensure the event belongs to the topic {topic} and try to get at least {numevents} unique events possible from the context.
|
| 62 |
+
|
| 63 |
Output:
|
| 64 |
"""
|
| 65 |
|
|
|
|
| 71 |
|
| 72 |
# Function to get the appropriate language model based on user selection
|
| 73 |
def getModel(model, key):
|
| 74 |
+
if(model == 'OpenAI Gpt-o'):
|
| 75 |
os.environ['OPENAI_API_KEY'] = key
|
| 76 |
return ChatOpenAI(temperature=0, # Set to 0 for deterministic output
|
| 77 |
model="gpt-4o-2024-08-06", # Using the GPT-4 Turbo model
|
| 78 |
max_tokens=8000) # Limit the response length
|
| 79 |
+
elif (model == 'Anthropic Claude'):
|
| 80 |
os.environ['ANTHROPIC_API_KEY'] = key
|
| 81 |
return ChatAnthropic(model='claude-3-5-sonnet-20240620') # Limit the response length
|
| 82 |
+
else:
|
| 83 |
+
os.environ['GOOGLE_API_KEY'] = key
|
| 84 |
+
return ChatGoogleGenerativeAI(
|
| 85 |
+
model="gemini-1.5-pro",
|
| 86 |
+
temperature=0,
|
| 87 |
+
max_tokens=8000,
|
| 88 |
+
max_retries=2,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
|
| 92 |
|
| 93 |
# Function to generate an HTML table from the summary object
|
|
|
|
| 109 |
for idx, tline in timeline.iterrows():
|
| 110 |
if idx % 2 == 0:
|
| 111 |
rows.append(Li(Div(File("./assets/circle.svg"), cls = "timeline-middle"),
|
| 112 |
+
Div(Time(tline['TimeStr'],
|
| 113 |
+
cls = "font-mono italic"),
|
| 114 |
+
Div(tline['Event'],
|
| 115 |
+
cls = 'text-lg font-black'),
|
| 116 |
+
cls = "timeline-start mb-10 md:text-end"),
|
| 117 |
Hr()))
|
| 118 |
else:
|
| 119 |
rows.append(Li(Div(File("./assets/circle.svg"), cls = "timeline-middle"),
|
| 120 |
+
Div(Time(tline['TimeStr'],
|
| 121 |
+
cls = "font-mono italic"),
|
| 122 |
+
Div(tline['Event'],
|
| 123 |
+
cls = 'text-lg font-black'),
|
| 124 |
+
cls = "timeline-end mb-10"),
|
| 125 |
Hr()))
|
| 126 |
|
| 127 |
return Ul(*rows, cls="timeline timeline-vertical")
|
|
|
|
| 160 |
return df
|
| 161 |
|
| 162 |
# Placeholder function for Q&A generation
|
| 163 |
+
def generate_timeline(topic, numevents, llm):
|
| 164 |
# This function will be implemented later
|
| 165 |
# For now, return a sample DataFrame
|
| 166 |
+
|
| 167 |
+
# titles = wikipedia.search(topic, results = 1)
|
| 168 |
+
# page = wikipedia.page(titles[0])
|
| 169 |
+
# wiki_content = page.content
|
| 170 |
+
|
| 171 |
|
| 172 |
+
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=5000))
|
| 173 |
wiki_content = wikipedia.run(topic)
|
| 174 |
+
|
| 175 |
+
print(f"wiki_content: {wiki_content}")
|
| 176 |
+
# print(f"wiki_artifact: {wiki_artifact}")
|
| 177 |
|
| 178 |
chain = event_prompt | llm | parser
|
| 179 |
|
| 180 |
+
result = chain.invoke({"context" : wiki_content,
|
| 181 |
+
"topic": topic,
|
| 182 |
+
"numevents": numevents})
|
| 183 |
|
| 184 |
try:
|
| 185 |
# Parse the output using PydanticOutputParser
|
|
|
|
| 209 |
),
|
| 210 |
Div(
|
| 211 |
Span(Strong('Model: '), cls ="badge"),
|
| 212 |
+
Select(Option("OpenAI Gpt-4o"), Option("Anthropic Claude"), Option("Google Gemini"), id="model", cls = 'select w-full max-w-xs')
|
| 213 |
),
|
| 214 |
Div(
|
| 215 |
Span(Strong('Topic for timeline (Person/Organization/Event): '), cls ="badge"),
|
|
|
|
| 222 |
cls = "input w-full max-w-xs",
|
| 223 |
placeholder = "Type here")
|
| 224 |
),
|
| 225 |
+
Div(
|
| 226 |
+
Span(Strong('How many events: '), cls ="badge"),
|
| 227 |
+
Select(Option("5"), Option("10"), Option("20"), Option("30"), id="numevents", cls = 'select w-full max-w-xs')
|
| 228 |
+
),
|
| 229 |
Div(
|
| 230 |
Button("Generate Timeline", cls = 'btn')
|
| 231 |
),
|
|
|
|
| 266 |
model = getModel(d['model'], d['secret'])
|
| 267 |
|
| 268 |
# Perform one-pass summarization
|
| 269 |
+
timeline_df = generate_timeline(d['topic'],
|
| 270 |
+
d['numevents'],
|
| 271 |
+
model)
|
| 272 |
#qas = pd.read_csv("results_tesla.csv")
|
| 273 |
|
| 274 |
timeline_df.head(10)
|
timeline.csv
CHANGED
|
@@ -1,21 +1,16 @@
|
|
| 1 |
,index,Time,Event,Sentiment,TimeStr
|
| 2 |
-
0,
|
| 3 |
-
1,
|
| 4 |
-
2,
|
| 5 |
-
3,
|
| 6 |
-
4,
|
| 7 |
-
5,
|
| 8 |
-
6,
|
| 9 |
-
7,
|
| 10 |
-
8,
|
| 11 |
-
9,
|
| 12 |
-
10,
|
| 13 |
-
11,
|
| 14 |
-
12,
|
| 15 |
-
13,
|
| 16 |
-
14,
|
| 17 |
-
15,13,2018-05-27 00:00:00+00:00,CSK won the IPL for the third time under Dhoni.,Positive,27/05/2018
|
| 18 |
-
16,5,2018-09-28 00:00:00+00:00,Dhoni was part of the 2018 Asia Cup winning squad.,Positive,28/09/2018
|
| 19 |
-
17,10,2019-07-10 00:00:00+00:00,Dhoni retired from international limited-overs cricket.,Negative,10/07/2019
|
| 20 |
-
18,14,2021-10-15 00:00:00+00:00,CSK won the IPL for the fourth time with Dhoni.,Positive,15/10/2021
|
| 21 |
-
19,15,2023-05-29 00:00:00+00:00,CSK won the IPL for the fifth time under Dhoni.,Positive,29/05/2023
|
|
|
|
| 1 |
,index,Time,Event,Sentiment,TimeStr
|
| 2 |
+
0,0,1981-07-07 00:00:00+00:00,MS Dhoni is born,Positive,07/07/1981
|
| 3 |
+
1,1,1999-01-01 00:00:00+00:00,Dhoni makes his first class debut for Bihar,Positive,01/01/1999
|
| 4 |
+
2,2,2004-12-23 00:00:00+00:00,Dhoni makes his debut for the Indian cricket team in an ODI against Bangladesh,Positive,23/12/2004
|
| 5 |
+
3,3,2005-12-01 00:00:00+00:00,Dhoni plays his first test match against Sri Lanka,Positive,01/12/2005
|
| 6 |
+
4,4,2007-01-01 00:00:00+00:00,Dhoni becomes captain of the ODI side,Positive,01/01/2007
|
| 7 |
+
5,5,2007-09-24 00:00:00+00:00,Dhoni leads India to victory in the ICC World Twenty20,Positive,24/09/2007
|
| 8 |
+
6,6,2008-01-01 00:00:00+00:00,Dhoni takes over as captain in all formats of cricket,Positive,01/01/2008
|
| 9 |
+
7,7,2008-01-01 00:00:00+00:00,Dhoni is awarded India's highest sport honor Major Dhyan Chand Khel Ratna Award,Positive,01/01/2008
|
| 10 |
+
8,8,2009-01-01 00:00:00+00:00,"Dhoni receives the Padma Shri, India's fourth highest civilian award",Positive,01/01/2009
|
| 11 |
+
9,10,2011-01-01 00:00:00+00:00,Dhoni is awarded honorary rank of Lieutenant Colonel in the Indian Territorial Army,Positive,01/01/2011
|
| 12 |
+
10,9,2011-04-02 00:00:00+00:00,Dhoni leads India to victory in the Cricket World Cup,Positive,02/04/2011
|
| 13 |
+
11,11,2013-06-23 00:00:00+00:00,Dhoni leads India to victory in the ICC Champions Trophy,Positive,23/06/2013
|
| 14 |
+
12,12,2014-12-30 00:00:00+00:00,Dhoni retires from test cricket,Negative,30/12/2014
|
| 15 |
+
13,13,2018-01-01 00:00:00+00:00,"Dhoni receives the Padma Bhushan, India's third highest civilian award",Positive,01/01/2018
|
| 16 |
+
14,14,2019-12-31 00:00:00+00:00,Dhoni retires from limited overs international cricket,Negative,31/12/2019
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|