Derfel2025 commited on
Commit
6f4a9dd
·
0 Parent(s):

Initial clean commit without secrets

Browse files
Files changed (4) hide show
  1. .gitattributes +35 -0
  2. .gitignore +1 -0
  3. README.md +13 -0
  4. app.py +218 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Hackathon
3
+ emoji: 🏆
4
+ colorFrom: indigo
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 5.34.2
8
+ app_file: app.py
9
+ pinned: false
10
+ short_description: hf space for generating legal analysis for hack the law
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
15
+ load_dotenv()
16
+
17
+ newsApiKey = os.getenv('NEWSAPI_KEY')
18
+ #grok_api_key = os.getenv('GROK_API_KEY')
19
+ HF_TOKEN = os.getenv("HF_TOKEN")
20
+ login(token=HF_TOKEN)
21
+ COHERE_API_KEY = os.getenv('COHERE_API_KEY')
22
+
23
+ from groq import Groq
24
+
25
+ client = Groq(
26
+ api_key=os.environ.get("GROQ_API_KEY"),
27
+ )
28
+
29
+
30
+ @tool
31
+ def get_company_news_articles(company_name: str) -> dict:
32
+ """
33
+ fetch news articles about a company.
34
+
35
+ Args:
36
+ company_name (str): the company to get news articles about
37
+
38
+ Returns:
39
+ list: a list of articles about the company
40
+ """
41
+
42
+ response = requests.get(f'https://newsapi.org/v2/everything?q={company_name}&from=2025-06-01&sortBy=popularity&apiKey={newsApiKey}')
43
+ try:
44
+ data = response.json()
45
+ except Exception as e:
46
+ print("❌ Failed to parse JSON:", str(e))
47
+ return []
48
+
49
+ print("🔍 Full NewsAPI response:", data)
50
+
51
+ if data.get("status") != "ok":
52
+ print("❌ NewsAPI error:", data.get("message"))
53
+ return []
54
+
55
+
56
+ print(f"results from news api get request are: {data.get('articles')}")
57
+
58
+ articles = data.get('articles', [])
59
+ filtered_articles = [article for article in articles]
60
+ return filtered_articles
61
+
62
+
63
+
64
+ class OllamaLLM:
65
+ pass
66
+ #just borrow all code below, and switched out the api call for the api responses to localhost port
67
+
68
+ class grokLLM:
69
+ pass
70
+
71
+ class mistralLLM:
72
+ pass
73
+
74
+
75
+
76
+ class CohereLLM:
77
+ def __init__(self, api_key: str = COHERE_API_KEY, model_name: str = "command-r-plus"):
78
+ self.client = cohere.Client(api_key)
79
+ self.model_name = model_name
80
+
81
+ def __call__(self, prompt: str, **kwargs) -> str:
82
+ # Remove keys not supported by Cohere
83
+ kwargs.pop('stop_sequences', None)
84
+
85
+ if isinstance(prompt, list):
86
+ prompt = self._convert_chat_to_prompt(prompt)
87
+
88
+ # Optional: set temperature, max tokens, etc.
89
+ temperature = kwargs.get("temperature", 0.7)
90
+ max_tokens = kwargs.get("max_tokens", 300)
91
+
92
+ response = self.client.generate(
93
+ model=self.model_name,
94
+ prompt=prompt,
95
+ max_tokens=max_tokens,
96
+ temperature=temperature,
97
+ )
98
+ #return response.generations[0].text.strip()
99
+
100
+ output_text = response.generations[0].text.strip()
101
+
102
+ # ✅ Wrap response in ChatMessage
103
+ return ChatMessage(role="assistant", content=output_text)
104
+
105
+ def _convert_chat_to_prompt(self, messages):
106
+ """
107
+ Convert chat-style message history to a single string prompt.
108
+ Example input: [{"role": "user", "content": "Hello"}, {"role": "assistant", "content": "Hi!"}]
109
+ """
110
+ prompt = ""
111
+ for message in messages:
112
+ role = message.get("role", "user")
113
+ content = message.get("content", "")
114
+ if role == "user":
115
+ prompt += f"User: {content}\n"
116
+ elif role == "assistant":
117
+ prompt += f"Assistant: {content}\n"
118
+ else:
119
+ prompt += f"{role.title()}: {content}\n"
120
+ prompt += "Assistant:" # Final cue for model to respond
121
+ return prompt
122
+
123
+
124
+
125
+
126
+ cohere_model = CohereLLM()
127
+
128
+
129
+
130
+
131
+
132
+ #HfApiModel("mistralai/Mistral-7B-v0.1-chat")
133
+
134
+ def mainFunc(articles):
135
+ articles = json.loads(articles)
136
+ newsApiKey = os.getenv('NEWSAPI_KEY')
137
+ if not newsApiKey:
138
+ raise ValueError("Missing NEWS_API_KEY in environment variables.")
139
+
140
+ print(f"data structure of articles is: {articles}")
141
+
142
+ prompt = f"""
143
+ You are an agent that analyzes the risk factors for a company, by using the data from:
144
+ - Documents: {articles['company_info']['documents']}
145
+ - News summaries: {articles['news_data']['articles_summary']}
146
+
147
+ Please respond using **only** the following JSON format:
148
+
149
+ {{
150
+ "risk_factors": [
151
+ {{
152
+ "severity": "Low | Medium | High",
153
+ "specific_event": "the specific type of event, i. e. nuclear war"
154
+ "risk_type": "e.g., Operational Risk, Financial Risk, Reputational Risk, Legal Risk",
155
+ "affected_contracts": "Name or path of the affected document",
156
+ "affected_clauses": "Clause number(s) or section(s)",
157
+ "narrative": {{
158
+ "solutions_in_contract": "Summarize contractual protections or remedies",
159
+ "alternative_mitigation_strategies": "Suggest other ways to reduce the risk",
160
+ "monitoring_tasks": "Define ongoing monitoring or reporting actions"
161
+ }}
162
+ }}
163
+ ],
164
+ "available_data": {{
165
+ "context_items": ["Summary of the company info and risk-related context"],
166
+ "document_count": {len(articles['company_info']['documents'])},
167
+ "content_available": true,
168
+ "news_available": true
169
+ }}
170
+ }}
171
+ """
172
+
173
+
174
+ chat_completion = client.chat.completions.create(
175
+ messages=[
176
+ {
177
+ "role": "system",
178
+ "content": prompt
179
+ },
180
+ {
181
+ "role": "user",
182
+ "content": f"What are the {articles['analysis_request']['analysis_scope']} risk factors for {articles['company_info']['name']} ?",
183
+ }
184
+ ],
185
+ model="llama-3.3-70b-versatile",
186
+ response_format={"type": "json_object"},#and include word 'json' in messages/prompt
187
+ )
188
+ print(chat_completion.choices[0].message.content)
189
+ return chat_completion.choices[0].message.content
190
+
191
+
192
+
193
+ return result
194
+
195
+
196
+ #add agents it can hand off to
197
+
198
+
199
+
200
+
201
+
202
+ #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"
203
+ #agent.run("what are the most recent articles about Microsoft?")
204
+ #print(agent.prompt_templates["system_prompt"])
205
+
206
+
207
+ #huggingface-cli login - to set access token in temrainl and save it
208
+ #translation function works well
209
+
210
+ demo = gr.Interface(
211
+ fn=mainFunc,
212
+ inputs="text",
213
+ outputs="text",
214
+ title="hackathon agent",
215
+ description="finds info about a company"
216
+ )
217
+
218
+ demo.launch(share=True)