Spaces:
Build error
Build error
sainathBelagavi
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from huggingface_hub import InferenceClient, login
|
3 |
+
from transformers import HfAgent, HuggingFaceHubTool
|
4 |
+
import os
|
5 |
+
import pickle
|
6 |
+
|
7 |
+
# Initialize Hugging Face login
|
8 |
+
login(token=os.environ.get("HUGGINGFACE_API_KEY"))
|
9 |
+
|
10 |
+
# Initialize HfAgent
|
11 |
+
agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
|
12 |
+
|
13 |
+
# Add custom tools
|
14 |
+
date_extraction_tool = HuggingFaceHubTool("dslim/bert-base-NER")
|
15 |
+
claim_number_tool = HuggingFaceHubTool("distilbert-base-uncased-finetuned-sst-2-english")
|
16 |
+
call_purpose_tool = HuggingFaceHubTool("facebook/bart-large-mnli")
|
17 |
+
|
18 |
+
agent.add_tool(date_extraction_tool)
|
19 |
+
agent.add_tool(claim_number_tool)
|
20 |
+
agent.add_tool(call_purpose_tool)
|
21 |
+
|
22 |
+
st.title("Transcription Analysis Agent")
|
23 |
+
|
24 |
+
# Base URL for Inference API
|
25 |
+
BASE_URL = "https://api-inference.huggingface.co/models/"
|
26 |
+
|
27 |
+
model_links = {
|
28 |
+
"LegacyLift🚀": BASE_URL + "mistralai/Mistral-7B-Instruct-v0.3",
|
29 |
+
"ModernMigrate⭐": BASE_URL + "mistralai/Mixtral-8x7B-Instruct-v0.1",
|
30 |
+
"RetroRecode🔄": BASE_URL + "microsoft/Phi-3-mini-4k-instruct"
|
31 |
+
}
|
32 |
+
|
33 |
+
model_info = {
|
34 |
+
"LegacyLift🚀": {
|
35 |
+
'description': "The LegacyLift model is a **Large Language Model (LLM)** for problem-solving, content writing, and daily tips.",
|
36 |
+
'logo': './11.jpg'
|
37 |
+
},
|
38 |
+
"ModernMigrate⭐": {
|
39 |
+
'description': "The ModernMigrate model excels in coding, logical reasoning, and high-speed inference.",
|
40 |
+
'logo': './2.jpg'
|
41 |
+
},
|
42 |
+
"RetroRecode🔄": {
|
43 |
+
'description': "The RetroRecode model is ideal for critical development, practical knowledge, and serverless inference.",
|
44 |
+
'logo': './3.jpg'
|
45 |
+
},
|
46 |
+
}
|
47 |
+
|
48 |
+
def format_prompt(message, conversation_history, custom_instructions=None):
|
49 |
+
prompt = f"\[INST\] {custom_instructions} \[/INST\]\n\[CONV_HISTORY\]\n"
|
50 |
+
for role, content in conversation_history:
|
51 |
+
prompt += f"{role.upper()}: {content}\n"
|
52 |
+
prompt += f"\[/CONV_HISTORY\]\n\[INST\] {message} \[/INST\]\n\[RESPONSE\]\n"
|
53 |
+
return prompt
|
54 |
+
|
55 |
+
def reset_conversation():
|
56 |
+
st.session_state.conversation = []
|
57 |
+
st.session_state.messages = []
|
58 |
+
st.session_state.chat_state = "reset"
|
59 |
+
|
60 |
+
def load_conversation_history():
|
61 |
+
history_file = "conversation_history.pickle"
|
62 |
+
return pickle.load(open(history_file, "rb")) if os.path.exists(history_file) else []
|
63 |
+
|
64 |
+
def save_conversation_history(conversation_history):
|
65 |
+
with open("conversation_history.pickle", "wb") as f:
|
66 |
+
pickle.dump(conversation_history, f)
|
67 |
+
|
68 |
+
def analyze_transcript(transcript, model):
|
69 |
+
client = InferenceClient(model=model)
|
70 |
+
|
71 |
+
# Extract dates
|
72 |
+
dates_prompt = f"Extract dates from this text: {transcript}"
|
73 |
+
dates = client.text_generation(dates_prompt, max_new_tokens=50)
|
74 |
+
|
75 |
+
# Extract claim number
|
76 |
+
claim_prompt = f"Find the claim number in this text: {transcript}"
|
77 |
+
claim_number = client.text_generation(claim_prompt, max_new_tokens=50)
|
78 |
+
|
79 |
+
# Determine call purpose
|
80 |
+
purpose_prompt = f"What is the main purpose of this call based on the transcript: {transcript}"
|
81 |
+
call_purpose = client.text_generation(purpose_prompt, max_new_tokens=100)
|
82 |
+
|
83 |
+
return {
|
84 |
+
"dates": dates,
|
85 |
+
"claim_number": claim_number,
|
86 |
+
"call_purpose": call_purpose
|
87 |
+
}
|
88 |
+
|
89 |
+
# Sidebar UI
|
90 |
+
models = list(model_links.keys())
|
91 |
+
selected_model = st.sidebar.selectbox("Select Model", models)
|
92 |
+
temp_values = st.sidebar.slider('Select Temperature', 0.0, 1.0, 0.5)
|
93 |
+
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
94 |
+
|
95 |
+
st.sidebar.write(f"Chatting with **{selected_model}**")
|
96 |
+
st.sidebar.markdown(model_info[selected_model]['description'])
|
97 |
+
st.sidebar.image(model_info[selected_model]['logo'])
|
98 |
+
|
99 |
+
# Load session state
|
100 |
+
if "prev_option" not in st.session_state:
|
101 |
+
st.session_state.prev_option = selected_model
|
102 |
+
|
103 |
+
if st.session_state.prev_option != selected_model:
|
104 |
+
st.session_state.messages = []
|
105 |
+
st.session_state.prev_option = selected_model
|
106 |
+
|
107 |
+
if "chat_state" not in st.session_state:
|
108 |
+
st.session_state.chat_state = "normal"
|
109 |
+
|
110 |
+
if "messages" not in st.session_state:
|
111 |
+
st.session_state.messages = load_conversation_history()
|
112 |
+
|
113 |
+
# Main Chat
|
114 |
+
repo_id = model_links[selected_model]
|
115 |
+
st.subheader(f"{selected_model}")
|
116 |
+
|
117 |
+
if st.session_state.chat_state == "normal":
|
118 |
+
for message in st.session_state.messages:
|
119 |
+
with st.chat_message(message["role"]):
|
120 |
+
st.markdown(message["content"])
|
121 |
+
|
122 |
+
if prompt := st.chat_input(f"Hi I'm {selected_model}, How can I help you today?"):
|
123 |
+
custom_instruction = (
|
124 |
+
"Analyze this transcript with precision. Remove commas in claim numbers, preserve exact numbers and "
|
125 |
+
"dates (dd/mm/yy). Extract claim numbers as single entities."
|
126 |
+
"Analyze this transcript with precision. Remove commas in claim numbers, preserve exact numbers and "
|
127 |
+
"dates (dd/mm/yy). Extract claim numbers as single entities."
|
128 |
+
"""1. Only include information explicitly stated
|
129 |
+
2. Mark unclear information as "UNCLEAR"
|
130 |
+
|
131 |
+
3. Preserve exact numbers, dates (in dd/mm/yy format), and claims
|
132 |
+
|
133 |
+
4. Focus on factual content
|
134 |
+
|
135 |
+
IMPORTANT REQUIREMENTS:
|
136 |
+
|
137 |
+
- Format all dates as dd/mm/yy
|
138 |
+
|
139 |
+
- Extract and list all claim numbers mentioned
|
140 |
+
|
141 |
+
- Maintain exact numbers and statistics as stated
|
142 |
+
|
143 |
+
- Do not make assumptions about unclear information
|
144 |
+
|
145 |
+
Please analyze the following transcript and structure your response as follows:
|
146 |
+
|
147 |
+
PARTICIPANTS:
|
148 |
+
|
149 |
+
- List all participants and their roles (if mentioned)
|
150 |
+
|
151 |
+
CONTEXT:
|
152 |
+
|
153 |
+
- Meeting purpose
|
154 |
+
|
155 |
+
- Duration (if mentioned)
|
156 |
+
|
157 |
+
- Meeting date/time
|
158 |
+
|
159 |
+
KEY POINTS:
|
160 |
+
|
161 |
+
- Main topics discussed
|
162 |
+
|
163 |
+
- Decisions made
|
164 |
+
|
165 |
+
- Important numbers/metrics mentioned
|
166 |
+
|
167 |
+
- Claims discussed
|
168 |
+
|
169 |
+
ACTION ITEMS:
|
170 |
+
|
171 |
+
- Specific tasks assigned
|
172 |
+
|
173 |
+
- Who is responsible
|
174 |
+
|
175 |
+
- Deadlines (in dd/mm/yy format)
|
176 |
+
|
177 |
+
FOLLOW UP:
|
178 |
+
|
179 |
+
- Scheduled next meetings
|
180 |
+
|
181 |
+
- Pending items
|
182 |
+
|
183 |
+
- Required approvals or confirmations"""
|
184 |
+
# ... (rest of the custom instructions)
|
185 |
+
)
|
186 |
+
|
187 |
+
conversation_history = [(msg["role"], msg["content"]) for msg in st.session_state.messages]
|
188 |
+
formatted_text = format_prompt(prompt, conversation_history, custom_instruction)
|
189 |
+
|
190 |
+
with st.chat_message("user"):
|
191 |
+
st.markdown(prompt)
|
192 |
+
|
193 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
194 |
+
|
195 |
+
with st.chat_message("assistant"):
|
196 |
+
try:
|
197 |
+
client = InferenceClient(model=repo_id)
|
198 |
+
response = client.text_generation(
|
199 |
+
formatted_text,
|
200 |
+
temperature=temp_values,
|
201 |
+
max_new_tokens=1024,
|
202 |
+
stream=True
|
203 |
+
)
|
204 |
+
response_text = ''.join(response)
|
205 |
+
response_text = response_text.replace(",", "")
|
206 |
+
st.markdown(response_text)
|
207 |
+
|
208 |
+
# Use the agent to analyze the transcript
|
209 |
+
analysis_results = analyze_transcript(prompt, repo_id)
|
210 |
+
st.markdown("### Transcript Analysis")
|
211 |
+
st.json(analysis_results)
|
212 |
+
|
213 |
+
st.session_state.messages.append({"role": "assistant", "content": response_text})
|
214 |
+
save_conversation_history(st.session_state.messages)
|
215 |
+
except Exception as e:
|
216 |
+
st.error(f"An error occurred: {e}")
|
217 |
+
|
218 |
+
elif st.session_state.chat_state == "reset":
|
219 |
+
st.session_state.chat_state = "normal"
|
220 |
+
st.experimental_rerun()
|