Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,71 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
import pandas as pd
|
|
|
|
|
5 |
from typing import List, Tuple
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
def analyze_file_content(content, file_type):
|
19 |
"""Analyze file content and return structural summary"""
|
@@ -94,7 +146,7 @@ def format_history(history):
|
|
94 |
formatted_history.append({"role": "assistant", "content": assistant_msg})
|
95 |
return formatted_history
|
96 |
|
97 |
-
def chat(message, history, uploaded_file,
|
98 |
system_prefix = """You are a file analysis expert. Analyze the uploaded file in depth from the following perspectives:
|
99 |
1. π Overall structure and composition
|
100 |
2. π Key content and pattern analysis
|
@@ -103,7 +155,6 @@ def chat(message, history, uploaded_file, model_name, system_message="", max_tok
|
|
103 |
- For text/code: Structural features, main patterns
|
104 |
4. π‘ Potential applications
|
105 |
5. β¨ Data quality and areas for improvement
|
106 |
-
|
107 |
Provide detailed and structured analysis from an expert perspective, but explain in an easy-to-understand way. Format the analysis results in Markdown and include specific examples where possible."""
|
108 |
|
109 |
if uploaded_file:
|
@@ -120,7 +171,6 @@ Provide detailed and structured analysis from an expert perspective, but explain
|
|
120 |
|
121 |
if message == "Starting file analysis...":
|
122 |
message = f"""[Structure Analysis] {file_summary}
|
123 |
-
|
124 |
Please provide detailed analysis from these perspectives:
|
125 |
1. π Overall file structure and format
|
126 |
2. π Key content and component analysis
|
@@ -144,7 +194,7 @@ Please provide detailed analysis from these perspectives:
|
|
144 |
messages.append({"role": "user", "content": message})
|
145 |
|
146 |
try:
|
147 |
-
client =
|
148 |
partial_message = ""
|
149 |
current_history = []
|
150 |
|
@@ -176,13 +226,12 @@ css = """
|
|
176 |
footer {visibility: hidden}
|
177 |
"""
|
178 |
|
179 |
-
|
180 |
-
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, title="Every RAG π€") as demo:
|
181 |
gr.HTML(
|
182 |
"""
|
183 |
<div style="text-align: center; max-width: 800px; margin: 0 auto;">
|
184 |
-
<h1 style="font-size: 3em; font-weight: 600; margin: 0.5em;">
|
185 |
-
<h3 style="font-size: 1.2em; margin: 1em;">
|
186 |
</div>
|
187 |
"""
|
188 |
)
|
@@ -197,7 +246,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, title="Every RAG
|
|
197 |
msg = gr.Textbox(
|
198 |
label="Type your message",
|
199 |
show_label=False,
|
200 |
-
placeholder="Ask me anything about the uploaded file... π",
|
201 |
container=False
|
202 |
)
|
203 |
with gr.Row():
|
@@ -205,13 +254,6 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, title="Every RAG
|
|
205 |
send = gr.Button("Send π€")
|
206 |
|
207 |
with gr.Column(scale=1):
|
208 |
-
model_name = gr.Radio(
|
209 |
-
choices=list(LLM_MODELS.keys()),
|
210 |
-
value="Cohere c4ai-crp-08-2024",
|
211 |
-
label="Select LLM Model π€",
|
212 |
-
info="Choose your preferred AI model"
|
213 |
-
)
|
214 |
-
|
215 |
gr.Markdown("### Upload File π\nSupport: Text, Code, CSV, Parquet files")
|
216 |
file_upload = gr.File(
|
217 |
label="Upload File",
|
@@ -228,7 +270,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, title="Every RAG
|
|
228 |
# Event bindings
|
229 |
msg.submit(
|
230 |
chat,
|
231 |
-
inputs=[msg, chatbot, file_upload,
|
232 |
outputs=[msg, chatbot],
|
233 |
queue=True
|
234 |
).then(
|
@@ -239,7 +281,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, title="Every RAG
|
|
239 |
|
240 |
send.click(
|
241 |
chat,
|
242 |
-
inputs=[msg, chatbot, file_upload,
|
243 |
outputs=[msg, chatbot],
|
244 |
queue=True
|
245 |
).then(
|
@@ -251,7 +293,7 @@ with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css=css, title="Every RAG
|
|
251 |
# Auto-analysis on file upload
|
252 |
file_upload.change(
|
253 |
chat,
|
254 |
-
inputs=[gr.Textbox(value="Starting file analysis..."), chatbot, file_upload,
|
255 |
outputs=[msg, chatbot],
|
256 |
queue=True
|
257 |
)
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
import pandas as pd
|
4 |
+
import requests
|
5 |
+
import json
|
6 |
from typing import List, Tuple
|
7 |
|
8 |
+
class OllamaClient:
|
9 |
+
def __init__(self, model_name: str = "mistral-nemo", base_url: str = "http://localhost:11434"):
|
10 |
+
self.model_name = model_name
|
11 |
+
self.base_url = base_url
|
12 |
+
|
13 |
+
def chat_completion(self, messages, max_tokens=4000, stream=True, temperature=0.7, top_p=0.9):
|
14 |
+
# Convert messages to Ollama format
|
15 |
+
ollama_messages = []
|
16 |
+
for msg in messages:
|
17 |
+
if msg["role"] == "system":
|
18 |
+
ollama_messages.append({"role": "system", "content": msg["content"]})
|
19 |
+
elif msg["role"] in ["user", "assistant"]:
|
20 |
+
ollama_messages.append({"role": msg["role"], "content": msg["content"]})
|
21 |
+
|
22 |
+
# Prepare the request data
|
23 |
+
data = {
|
24 |
+
"model": self.model_name,
|
25 |
+
"messages": ollama_messages,
|
26 |
+
"options": {
|
27 |
+
"temperature": temperature,
|
28 |
+
"top_p": top_p,
|
29 |
+
"num_predict": max_tokens
|
30 |
+
},
|
31 |
+
"stream": stream
|
32 |
+
}
|
33 |
+
|
34 |
+
# Make the request to Ollama API
|
35 |
+
response = requests.post(
|
36 |
+
f"{self.base_url}/api/chat",
|
37 |
+
json=data,
|
38 |
+
stream=stream
|
39 |
+
)
|
40 |
+
|
41 |
+
if response.status_code != 200:
|
42 |
+
raise Exception(f"Ollama API error: {response.text}")
|
43 |
+
|
44 |
+
if stream:
|
45 |
+
for line in response.iter_lines():
|
46 |
+
if line:
|
47 |
+
decoded_line = line.decode('utf-8')
|
48 |
+
try:
|
49 |
+
chunk = json.loads(decoded_line)
|
50 |
+
if "message" in chunk:
|
51 |
+
yield {
|
52 |
+
"choices": [{
|
53 |
+
"delta": {
|
54 |
+
"content": chunk["message"]["content"]
|
55 |
+
}
|
56 |
+
}]
|
57 |
+
}
|
58 |
+
except json.JSONDecodeError:
|
59 |
+
continue
|
60 |
+
else:
|
61 |
+
result = response.json()
|
62 |
+
yield {
|
63 |
+
"choices": [{
|
64 |
+
"delta": {
|
65 |
+
"content": result["message"]["content"]
|
66 |
+
}
|
67 |
+
}]
|
68 |
+
}
|
69 |
|
70 |
def analyze_file_content(content, file_type):
|
71 |
"""Analyze file content and return structural summary"""
|
|
|
146 |
formatted_history.append({"role": "assistant", "content": assistant_msg})
|
147 |
return formatted_history
|
148 |
|
149 |
+
def chat(message, history, uploaded_file, system_message="", max_tokens=4000, temperature=0.7, top_p=0.9):
|
150 |
system_prefix = """You are a file analysis expert. Analyze the uploaded file in depth from the following perspectives:
|
151 |
1. π Overall structure and composition
|
152 |
2. π Key content and pattern analysis
|
|
|
155 |
- For text/code: Structural features, main patterns
|
156 |
4. π‘ Potential applications
|
157 |
5. β¨ Data quality and areas for improvement
|
|
|
158 |
Provide detailed and structured analysis from an expert perspective, but explain in an easy-to-understand way. Format the analysis results in Markdown and include specific examples where possible."""
|
159 |
|
160 |
if uploaded_file:
|
|
|
171 |
|
172 |
if message == "Starting file analysis...":
|
173 |
message = f"""[Structure Analysis] {file_summary}
|
|
|
174 |
Please provide detailed analysis from these perspectives:
|
175 |
1. π Overall file structure and format
|
176 |
2. π Key content and component analysis
|
|
|
194 |
messages.append({"role": "user", "content": message})
|
195 |
|
196 |
try:
|
197 |
+
client = OllamaClient()
|
198 |
partial_message = ""
|
199 |
current_history = []
|
200 |
|
|
|
226 |
footer {visibility: hidden}
|
227 |
"""
|
228 |
|
229 |
+
with gr.Blocks(theme="gstaff/xkcd", css=css, title="Offline Survey Data Analysis π") as demo:
|
|
|
230 |
gr.HTML(
|
231 |
"""
|
232 |
<div style="text-align: center; max-width: 800px; margin: 0 auto;">
|
233 |
+
<h1 style="font-size: 3em; font-weight: 600; margin: 0.5em;">Offline Survey Data Analysis</h1>
|
234 |
+
<h3 style="font-size: 1.2em; margin: 1em;">Leveraging Mistral-Nemo via Ollama</h3>
|
235 |
</div>
|
236 |
"""
|
237 |
)
|
|
|
246 |
msg = gr.Textbox(
|
247 |
label="Type your message",
|
248 |
show_label=False,
|
249 |
+
placeholder="Ask me anything about the uploaded data file... π",
|
250 |
container=False
|
251 |
)
|
252 |
with gr.Row():
|
|
|
254 |
send = gr.Button("Send π€")
|
255 |
|
256 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
gr.Markdown("### Upload File π\nSupport: Text, Code, CSV, Parquet files")
|
258 |
file_upload = gr.File(
|
259 |
label="Upload File",
|
|
|
270 |
# Event bindings
|
271 |
msg.submit(
|
272 |
chat,
|
273 |
+
inputs=[msg, chatbot, file_upload, system_message, max_tokens, temperature, top_p],
|
274 |
outputs=[msg, chatbot],
|
275 |
queue=True
|
276 |
).then(
|
|
|
281 |
|
282 |
send.click(
|
283 |
chat,
|
284 |
+
inputs=[msg, chatbot, file_upload, system_message, max_tokens, temperature, top_p],
|
285 |
outputs=[msg, chatbot],
|
286 |
queue=True
|
287 |
).then(
|
|
|
293 |
# Auto-analysis on file upload
|
294 |
file_upload.change(
|
295 |
chat,
|
296 |
+
inputs=[gr.Textbox(value="Starting file analysis..."), chatbot, file_upload, system_message, max_tokens, temperature, top_p],
|
297 |
outputs=[msg, chatbot],
|
298 |
queue=True
|
299 |
)
|