Spaces:
Runtime error
Runtime error
Commit ·
fc8c847
1
Parent(s): c790567
first1
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import login
|
| 2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool, Tool, load_tool, InferenceClientModel
|
| 3 |
+
from smolagents.models import ChatMessage
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import cohere
|
| 6 |
+
from gradio_client import Client
|
| 7 |
+
from newsapi import NewsApiClient
|
| 8 |
+
import requests
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import os
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
import json
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
from mistralai import Mistral
|
| 15 |
+
|
| 16 |
+
load_dotenv()
|
| 17 |
+
|
| 18 |
+
newsApiKey = os.getenv('NEWSAPI_KEY')
|
| 19 |
+
#grok_api_key = os.getenv('GROK_API_KEY')
|
| 20 |
+
#HF_TOKEN = os.getenv("HF_TOKEN")
|
| 21 |
+
#login(token=HF_TOKEN)
|
| 22 |
+
COHERE_API_KEY = os.getenv('COHERE_API_KEY')
|
| 23 |
+
|
| 24 |
+
from groq import Groq
|
| 25 |
+
|
| 26 |
+
#client = Groq(
|
| 27 |
+
#api_key=os.environ.get("GROQ_API_KEY"),
|
| 28 |
+
#)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
api_key = os.environ["MISTRAL_API_KEY"]
|
| 33 |
+
model = "mistral-large-latest"
|
| 34 |
+
|
| 35 |
+
client = Mistral(api_key=api_key)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
#HfApiModel("mistralai/Mistral-7B-v0.1-chat")
|
| 40 |
+
|
| 41 |
+
def mainFunc(articles, risk_factor):
|
| 42 |
+
if isinstance(articles, str):
|
| 43 |
+
articles = json.loads(articles)
|
| 44 |
+
newsApiKey = os.getenv('NEWSAPI_KEY')
|
| 45 |
+
if not newsApiKey:
|
| 46 |
+
raise ValueError("Missing NEWS_API_KEY in environment variables.")
|
| 47 |
+
|
| 48 |
+
print(f"data structure of articles is: {articles}")
|
| 49 |
+
|
| 50 |
+
prompt = f"""
|
| 51 |
+
You are an agent that analyzes the risk factors for a company, by using the data from:
|
| 52 |
+
- Documents: {articles['company_info']['documents']}
|
| 53 |
+
- News summaries: {articles['news_data']['articles_summary']}. The user wants to know whether a specific risk factor exists: {risk_factor}. Use the information you are provided to evaluate whether it does. respond in the format of yes/no, and then provide reason/s.
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
chat_completion = client.chat.complete(
|
| 60 |
+
model=model,
|
| 61 |
+
messages=[
|
| 62 |
+
{
|
| 63 |
+
"role": "system",
|
| 64 |
+
"content": prompt
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"role": "user",
|
| 68 |
+
"content": f"{risk_factor}",
|
| 69 |
+
}
|
| 70 |
+
],
|
| 71 |
+
#and include word 'json' in messages/prompt
|
| 72 |
+
)
|
| 73 |
+
print(chat_completion.choices[0].message.content)
|
| 74 |
+
return chat_completion.choices[0].message.content
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
return result
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
#add agents it can hand off to
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
#agent.prompt_templates["system_prompt"] = agent.prompt_templates["system_prompt"] + "\n when asked for most recent articles, return each article with its dict/list values, rather than just the title"
|
| 88 |
+
#agent.run("what are the most recent articles about Microsoft?")
|
| 89 |
+
#print(agent.prompt_templates["system_prompt"])
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
#huggingface-cli login - to set access token in temrainl and save it
|
| 93 |
+
#translation function works well
|
| 94 |
+
|
| 95 |
+
demo = gr.Interface(
|
| 96 |
+
fn=mainFunc,
|
| 97 |
+
inputs=["text", "text"],
|
| 98 |
+
outputs="text",
|
| 99 |
+
title="dynamic specific risk",
|
| 100 |
+
description="finds info about a company"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
demo.launch(share=True)
|