Divyansh Kushwaha
commited on
Commit
·
fd6a623
1
Parent(s):
5011037
UPdated
Browse files
api.py
CHANGED
@@ -323,35 +323,24 @@ def perform_sentiment_analysis(news_data):
|
|
323 |
return news_data, sentiment_counts
|
324 |
|
325 |
def extract_topics_with_hf(news_data):
|
|
|
326 |
structured_data = {
|
327 |
"Company": news_data.get("Company", "Unknown"),
|
328 |
"Articles": []
|
329 |
}
|
|
|
330 |
articles = news_data.get("Articles", [])
|
331 |
for article in articles:
|
332 |
content = f"{article['Title']} {article['Summary']}"
|
333 |
-
|
334 |
-
|
335 |
-
Analyze the following content: "{content}"
|
336 |
-
Extract and return **exactly three key topics** most relevant to this content.
|
337 |
-
The topics should be of one word after analyzing the content.
|
338 |
-
Respond in a JSON format like this:
|
339 |
-
{{"Topics": ["topic1", "topic2", "topic3"]}}
|
340 |
-
"""
|
341 |
-
try:
|
342 |
-
# Use Groq AI to invoke the model
|
343 |
-
response = llm.invoke([HumanMessage(content=prompt)]).content
|
344 |
-
topics_result = json.loads(response).get("Topics", ["Unknown"]) # Parse JSON response
|
345 |
-
except Exception as e:
|
346 |
-
print(f"Error while extracting topics: {e}")
|
347 |
-
topics_result = ["Unknown"]
|
348 |
|
349 |
structured_data["Articles"].append({
|
350 |
"Title": article["Title"],
|
351 |
"Summary": article["Summary"],
|
352 |
"Sentiment": article.get("Sentiment", "Unknown"),
|
353 |
"Score": article.get("Score", 0.0),
|
354 |
-
"Topics":
|
355 |
})
|
356 |
return structured_data
|
357 |
|
@@ -493,7 +482,7 @@ def generate_summary(company_name):
|
|
493 |
def home():
|
494 |
return {"message": "Welcome to the Company Sentiment API"}
|
495 |
|
496 |
-
@app.
|
497 |
def get_summary(company_name: str = Query(..., description="Enter company name")):
|
498 |
structured_summary = generate_summary(company_name)
|
499 |
return structured_summary
|
|
|
323 |
return news_data, sentiment_counts
|
324 |
|
325 |
def extract_topics_with_hf(news_data):
|
326 |
+
from transformers import pipeline
|
327 |
structured_data = {
|
328 |
"Company": news_data.get("Company", "Unknown"),
|
329 |
"Articles": []
|
330 |
}
|
331 |
+
topic_pipe = pipeline("text-classification", model="valurank/distilroberta-topic-classification",device=1)
|
332 |
articles = news_data.get("Articles", [])
|
333 |
for article in articles:
|
334 |
content = f"{article['Title']} {article['Summary']}"
|
335 |
+
topics_result = topic_pipe(content, top_k=3)
|
336 |
+
topics = [topic["label"] for topic in topics_result] if topics_result else ["Unknown"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
|
338 |
structured_data["Articles"].append({
|
339 |
"Title": article["Title"],
|
340 |
"Summary": article["Summary"],
|
341 |
"Sentiment": article.get("Sentiment", "Unknown"),
|
342 |
"Score": article.get("Score", 0.0),
|
343 |
+
"Topics": topics
|
344 |
})
|
345 |
return structured_data
|
346 |
|
|
|
482 |
def home():
|
483 |
return {"message": "Welcome to the Company Sentiment API"}
|
484 |
|
485 |
+
@app.post("/generateSummary")
|
486 |
def get_summary(company_name: str = Query(..., description="Enter company name")):
|
487 |
structured_summary = generate_summary(company_name)
|
488 |
return structured_summary
|