from huggingface_hub import login from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool, Tool, load_tool, InferenceClientModel from smolagents.models import ChatMessage from transformers import pipeline import cohere from gradio_client import Client from newsapi import NewsApiClient import requests import gradio as gr import os from dotenv import load_dotenv import json from transformers import pipeline from mistralai import Mistral load_dotenv() newsApiKey = os.getenv('NEWSAPI_KEY') #grok_api_key = os.getenv('GROK_API_KEY') #HF_TOKEN = os.getenv("HF_TOKEN") #login(token=HF_TOKEN) COHERE_API_KEY = os.getenv('COHERE_API_KEY') from groq import Groq #client = Groq( #api_key=os.environ.get("GROQ_API_KEY"), #) api_key = os.environ["MISTRAL_API_KEY"] model = "mistral-large-latest" client = Mistral(api_key=api_key) #HfApiModel("mistralai/Mistral-7B-v0.1-chat") def mainFunc(articles, risk_factor): if isinstance(articles, str): articles = json.loads(articles) newsApiKey = os.getenv('NEWSAPI_KEY') if not newsApiKey: raise ValueError("Missing NEWS_API_KEY in environment variables.") print(f"data structure of articles is: {articles}") prompt = f""" You are an agent that analyzes the risk factors for a company, by using the data from: - Documents: {articles['company_info']['documents']} - 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. """ chat_completion = client.chat.complete( model=model, messages=[ { "role": "system", "content": prompt }, { "role": "user", "content": f"{risk_factor}", } ], #and include word 'json' in messages/prompt ) print(chat_completion.choices[0].message.content) return chat_completion.choices[0].message.content return result #add agents it can hand off to #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" #agent.run("what are the most recent articles about Microsoft?") #print(agent.prompt_templates["system_prompt"]) #huggingface-cli login - to set access token in temrainl and save it #translation function works well demo = gr.Interface( fn=mainFunc, inputs=["text", "text"], outputs="text", title="dynamic specific risk", description="finds info about a company" ) demo.launch(share=True)