Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,105 +1,72 @@
|
|
1 |
-
|
2 |
-
from
|
3 |
-
from sqlalchemy import text
|
4 |
-
from llama_index.core import SQLDatabase
|
5 |
-
from llama_index.core.query_engine import NLSQLTableQueryEngine
|
6 |
-
from llama_index.llms.huggingface import HuggingFaceLLM
|
7 |
-
import logging
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
-
engine = create_engine("postgresql+psycopg2://postgres:password@localhost:5434/postgres")
|
15 |
|
16 |
-
metadata_obj = MetaData()
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
Column("CT_Avg", Float),
|
28 |
-
Column("total_current", Float),
|
29 |
-
Column("state", Text),
|
30 |
-
Column("state_duration", Integer),
|
31 |
-
Column("fault_status", Text),
|
32 |
-
Column("fw_version", Text),
|
33 |
-
Column("machineId", UUID),
|
34 |
-
Column("hi", Text),
|
35 |
-
)
|
36 |
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
with engine.connect() as conn:
|
42 |
-
conn.execute(text("SELECT create_hypertable('machine_current_log', 'created_at', if_not_exists => TRUE);"))
|
43 |
-
print("TimescaleDB hypertable created")
|
44 |
-
conn.commit()
|
45 |
|
46 |
-
|
47 |
-
print("\nQuerying all MAC addresses:")
|
48 |
-
with engine.connect() as con:
|
49 |
-
rows = con.execute(text("SELECT mac from machine_current_log"))
|
50 |
-
for row in rows:
|
51 |
-
print(row)
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
count_stmt = text("SELECT COUNT(*) FROM machine_current_log")
|
65 |
-
count = connection.execute(count_stmt).scalar()
|
66 |
-
print(f"Total number of rows in table: {count}")
|
67 |
-
results = connection.execute(stmt).fetchall()
|
68 |
-
print(results)
|
69 |
|
70 |
-
# Set up LlamaIndex natural language querying
|
71 |
-
sql_database = SQLDatabase(engine)
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
)
|
79 |
|
80 |
-
query_engine = NLSQLTableQueryEngine(
|
81 |
-
sql_database=sql_database,
|
82 |
-
tables=["machine_current_log"],
|
83 |
-
llm=llm
|
84 |
-
)
|
85 |
|
86 |
-
|
87 |
-
try:
|
88 |
-
response = query_engine.query(question)
|
89 |
-
return str(response)
|
90 |
-
except Exception as e:
|
91 |
-
logger.error(f"Query error: {e}")
|
92 |
-
return f"Error processing query: {str(e)}"
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
-
|
96 |
-
|
97 |
-
questions
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
print(f"\nQuestion: {question}")
|
105 |
-
print("Answer:", natural_language_query(question))
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
"""
|
5 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
+
"""
|
7 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
9 |
+
demo = gr.ChatInterface(..., title="Wiser AI Assistant")
|
|
|
10 |
|
|
|
11 |
|
12 |
+
def respond(
|
13 |
+
message,
|
14 |
+
history: list[tuple[str, str]],
|
15 |
+
system_message,
|
16 |
+
max_tokens,
|
17 |
+
temperature,
|
18 |
+
top_p,
|
19 |
+
):
|
20 |
+
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
for val in history:
|
23 |
+
if val[0]:
|
24 |
+
messages.append({"role": "user", "content": val[0]})
|
25 |
+
if val[1]:
|
26 |
+
messages.append({"role": "assistant", "content": val[1]})
|
27 |
|
28 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
response = ""
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
for message in client.chat_completion(
|
33 |
+
messages,
|
34 |
+
max_tokens=max_tokens,
|
35 |
+
stream=True,
|
36 |
+
temperature=temperature,
|
37 |
+
top_p=top_p,
|
38 |
+
):
|
39 |
+
token = message.choices[0].delta.content
|
40 |
|
41 |
+
response += token
|
42 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
43 |
|
|
|
|
|
44 |
|
45 |
+
"""
|
46 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
47 |
+
"""
|
48 |
+
demo = gr.ChatInterface(
|
49 |
+
respond,
|
50 |
+
title="🤖 Wiser AI Assistant",
|
51 |
+
description="Your smart manufacturing assistant powered by Wiser Machines. Ask me anything about automation, productivity, factory operations, or how Wiser can help!",
|
52 |
+
additional_inputs=[
|
53 |
+
gr.Textbox(value="You are Wiser, an AI assistant specializing in smart manufacturing and factory automation. Respond clearly, concisely, and use real-world manufacturing examples when needed.", label="System message"),
|
54 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
55 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
56 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
57 |
+
],
|
58 |
)
|
59 |
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
demo = gr.ChatInterface(..., title="Wiser AI Assistant")
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
+
with gr.Blocks() as demo:
|
65 |
+
gr.Markdown("## Welcome to Wiser AI Assistant")
|
66 |
+
gr.Markdown("Ask questions about factory automation, productivity, or how Wiser Machines can help streamline your operations.")
|
67 |
+
chat = gr.ChatInterface(
|
68 |
+
respond,
|
69 |
+
additional_inputs=[...], # your sliders and system message
|
70 |
+
)
|
71 |
+
|
72 |
+
demo.launch()
|
|
|
|