Kaynağa Gözat

初始化版本

zrd 3 ay önce
işleme
048f34153c
16 değiştirilmiş dosya ile 528 ekleme ve 0 silme
  1. 35 0
      .gitignore
  2. 30 0
      Dockerfile
  3. 9 0
      ai.iml
  4. 7 0
      docker-compose.yml
  5. 44 0
      main.py
  6. BIN
      requirements.txt
  7. 6 0
      utils/__init__.py
  8. 29 0
      utils/deepspeek.py
  9. 56 0
      utils/dmQuery.py
  10. 24 0
      utils/ollaiClient.py
  11. 29 0
      utils/orcUtils.py
  12. 29 0
      utils/readPdf.py
  13. 40 0
      utils/shellUtil.py
  14. 44 0
      utils/showdoc.py
  15. 49 0
      utils/snowflake.py
  16. 97 0
      utils/test.py

+ 35 - 0
.gitignore

@@ -0,0 +1,35 @@
+# 系统生成的文件
+.DS_Store
+Thumbs.db
+
+# Python 虚拟环境
+venv/
+env/
+.venv/
+
+# 编译的 Python 文件
+*.pyc
+__pycache__/
+
+# 日志文件
+*.log
+
+# 测试报告
+htmlcov/
+.coverage
+.coverage.*
+coverage.xml
+nosetests.xml
+
+# 打包相关文件
+dist/
+build/
+*.egg-info/
+
+# 配置文件
+.env
+settings_local.py
+
+# IDE 和编辑器的配置文件
+.idea/
+.vscode/

+ 30 - 0
Dockerfile

@@ -0,0 +1,30 @@
+# 使用 Python 基础镜像
+FROM python:3.10
+
+# 设置工作目录
+WORKDIR /app
+
+# 安装虚拟环境工具
+RUN apt-get update && apt-get install -y python3-venv
+
+# 创建虚拟环境
+RUN python3 -m venv venv
+
+# 激活虚拟环境并设置环境变量
+ENV PATH="/app/venv/bin:$PATH"
+
+# 复制项目文件到工作目录
+COPY . /app
+
+# 在虚拟环境中安装项目依赖
+RUN pip install --no-cache-dir -r requirements.txt
+
+RUN pip install uvicorn
+# 暴露应用端口
+#EXPOSE 5000
+
+# 启动应用
+
+CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "29015"]
+
+    

+ 9 - 0
ai.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="jdk" jdkName="Python 3.10 (venv)" jdkType="Python SDK" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 7 - 0
docker-compose.yml

@@ -0,0 +1,7 @@
+version: '3'
+services:
+  my_python_app:
+    build: .
+    ports:
+      - "29015:29015"
+    restart: always   

+ 44 - 0
main.py

@@ -0,0 +1,44 @@
+from fastapi import FastAPI
+from pydantic import BaseModel
+
+from utils import dmQuery
+from utils.shellUtil import execute_command_on_linux
+
+app = FastAPI() # 创建API实例
+
+@app.get("/")
+async def root():
+    return {"message": "Hello World"}
+
+
+
+
+
+# 将数据模型定义为继承BaseModel的类
+class Item(BaseModel):
+    sql: str
+
+
+
+@app.put("/exsql")
+async def create_item( item: Item, q: str = None):
+    result = {**item.dict()}
+    results = dmQuery.execute_query(item.sql)
+
+    return results
+
+
+
+@app.put("/exshell")
+async def create_item( item: Item, q: str = None):
+    # 配置 Linux 服务器信息
+    host = '119.29.146.254'
+    port = 22
+    username = 'root'
+    password = 'zrd1236987!'
+    # 要执行的命令
+
+    result = execute_command_on_linux(host, port, username, password, item.sql)
+
+    return result
+

BIN
requirements.txt


+ 6 - 0
utils/__init__.py

@@ -0,0 +1,6 @@
+from . import deepspeek
+from . import dmQuery
+from . import ollaiClient
+from . import readPdf
+from . import showdoc
+from . import snowflake

+ 29 - 0
utils/deepspeek.py

@@ -0,0 +1,29 @@
+# Please install OpenAI SDK first: `pip3 install openai`
+
+from openai import OpenAI
+
+
+def getAiResponse(question):
+    global response
+    client = OpenAI(api_key="sk-8255b8195cb542c0acec623afffd40a5", base_url="https://api.deepseek.com")
+    try:
+        response = client.chat.completions.create(
+            model="deepseek-chat",
+            messages=[
+                {"role": "system", "content": question},
+                {"role": "user", "content": "Hello"},
+            ],
+            stream=False
+        )
+        print(response)
+        if 'choices' in response and len(response.choices) > 0:
+            print(response.choices[0].message.content)
+            return response.choices[0].message.content
+        else:
+            return ''
+    except Exception as e:
+        print(f"Error: {e}")
+        return ''
+    finally:
+        if client:
+            client.close()

+ 56 - 0
utils/dmQuery.py

@@ -0,0 +1,56 @@
+import dmPython
+
+# 数据库连接配置
+config = {
+    "user": "DAM_CLOUD",  # 替换为你的用户名
+    "password": "DAM_CLOUD",  # 替换为你的密码
+    "host": "192.168.0.22",  # 替换为数据库主机地址,如localhost等
+    "port": 5555,  # 替换为达梦数据库的端口号,默认一般是5236
+}
+
+
+def execute_query(sql):
+    conn = None
+    result11 = None
+    try:
+        conn = dmPython.connect(**config)
+        cursor = conn.cursor()
+        cursor.execute(sql)
+        result11 = cursor.fetchall()
+    except dmPython.DatabaseError as e:
+        print(f"Database error: {e}")
+    except Exception as e:
+        print(f"Error: {e}")
+    finally:
+        if cursor:
+            cursor.close()
+        if conn:
+            conn.close()
+    return result11
+
+
+def execute_update(sql):
+    conn = None
+    try:
+        conn = dmPython.connect(**config)
+        cursor = conn.cursor()
+        cursor.execute(sql)
+        conn.commit()  # 提交事
+    except dmPython.DatabaseError as e:
+        print(f"Database error: {e}")
+    except Exception as e:
+        print(f"Error: {e}")
+    finally:
+        if cursor:
+            cursor.close()
+        if conn:
+            conn.close()
+
+
+if __name__ == "__main__":
+    excsql = (
+        f" select COLUMN_COMMENT,DICT_TYPE from DAM_CLOUD.INFRA_CODEGEN_COLUMN WHERE 1=1 and DELETED=0 limit 10; ")
+    results = execute_query(excsql)
+    sql_update = "UPDATE DAM_CITY.CITYPROD_UNIVERSITY SET LATITUDE='213' WHERE id=1892102544369856513;"
+    execute_update(sql_update)
+    print(results)

+ 24 - 0
utils/ollaiClient.py

@@ -0,0 +1,24 @@
+import ollama
+
+host = "127.0.0.1"
+port = "12434"
+client = ollama.Client(host=f"http://{host}:{port}")
+
+model1 = "deepseek-r1:8b"
+model2 = "deepseek-r1:1.5b"
+model3 = "qwen2:0.5b"
+
+
+def getAiResponse(question):
+    res = client.chat(model=model3,
+                      messages=[
+
+                          {"role": "user", "content": question}],
+                      options={"temperature": 0})
+
+    print(res.message.content)
+    return res
+
+
+if __name__ == "__main__":
+    getAiResponse("11111")

+ 29 - 0
utils/orcUtils.py

@@ -0,0 +1,29 @@
+import logging
+
+import pytesseract
+from PIL import Image
+
+# 配置日志记录
+logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
+
+# 如果 Tesseract-OCR 不在 PATH 中,需要指定其路径
+pytesseract.pytesseract.tesseract_cmd = r'G:\资料\tools\tesseract-ocr-master\Tesseract-OCR\tesseract.exe'
+
+# 打开图片文件
+image = Image.open('G:\\1111.jpg')
+
+try:
+    # 使用 pytesseract 进行 OCR 识别,并指定编码
+    text = pytesseract.image_to_string(image, lang='chi_sim')  # 如果图片是中文,可以指定语言
+    logging.info("OCR识别成功")
+except Exception as e:
+    logging.error(f"OCR识别失败: {e}")
+    text = ""
+
+# 打印识别的文字
+try:
+    print(text.encode('utf-8', errors='ignore').decode('utf-8'))
+except UnicodeDecodeError as e:
+    print(f"UnicodeDecodeError: {e}")
+    print("尝试使用 'replace' 处理编码错误")
+    print(text.encode('utf-8', errors='replace').decode('utf-8'))

+ 29 - 0
utils/readPdf.py

@@ -0,0 +1,29 @@
+import pdfplumber
+
+
+# 初始化 ollama.Client
+
+# 读取 PDF 文件并提取文本
+def extract_text_from_pdf(pdf_path):
+    text = ""
+    with pdfplumber.open(pdf_path) as pdf:
+        for page in pdf.pages:
+            text += page.extract_text()
+    return text
+
+
+def read_pdf(pdf_path):
+    # PDF 文件路径
+    # pdf_path = "path_to_your_pdf_file.pdf"
+
+    # 提取 PDF 文本
+    pdf_text = extract_text_from_pdf(pdf_path)
+
+    # 使用提取的文本作为问题材料
+    return pdf_text
+
+
+if __name__ == "__main__":
+    pdf_text1 = read_pdf('G:\\资料\\工作安排\\2月6日工作安排计划表-20250205.pdf')
+    # 打印响应
+    print(pdf_text1)

+ 40 - 0
utils/shellUtil.py

@@ -0,0 +1,40 @@
+import paramiko
+
+
+def execute_command_on_linux(host, port, username, password, command):
+    try:
+        # 创建 SSH 对象
+        ssh = paramiko.SSHClient()
+        # 允许连接不在 know_hosts 文件中的主机
+        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+        # 连接服务器
+        ssh.connect(hostname=host, port=port, username=username, password=password)
+        # 执行命令
+        stdin, stdout, stderr = ssh.exec_command(command)
+        # 获取命令结果
+        result = stdout.read().decode()
+        # 获取错误信息
+        error = stderr.read().decode()
+        if error:
+            print(f"执行命令时出现错误: {error}")
+        # 关闭连接
+        ssh.close()
+        return result
+    except Exception as e:
+        print(f"连接或执行命令时出现异常: {e}")
+        return None
+
+
+if __name__ == "__main__":
+    # 配置 Linux 服务器信息
+    host = '119.29.146.254'
+    port = 22
+    username = 'root'
+    password = 'zrd1236987!'
+    # 要执行的命令
+    command = 'du -sh /home/*'
+
+    result = execute_command_on_linux(host, port, username, password, command)
+    if result:
+        print("命令执行结果:")
+        print(result)

+ 44 - 0
utils/showdoc.py

@@ -0,0 +1,44 @@
+from datetime import datetime
+
+import requests
+
+
+def save_to_showdoc(cat_name, page_title, markdown_string):
+    # 接口的URL
+    url = "http://192.168.0.29:29011/server/index.php?s=/api/item/updateByApi"
+
+    # 请求头中添加api_key(这里假设按照你说的只是把这个链接作为api_key的值,实际可能需要根据真实情况调整)
+    headers = {
+        "Hm_lvt_4d9f9fa20dc17ceda35728278660ac7b": "1765869152623|1734332165"
+    }
+    try:
+        begin_date = datetime.now().strftime('%Y-%m-%d')
+        print(f"当天日期: {begin_date}")
+        # 构造POST请求的数据(示例数据,可替换为实际要发送的数据)
+        data = {
+            "api_key": "7d711192bea02ccd4f4733cc0e9f394c138081675",
+            "api_token": "f1e6f7f71bf25f0879fedef722bd90ea583708218",
+            "cat_name": cat_name,
+            "page_title": f"{page_title}-{begin_date}",
+            "page_content": markdown_string,
+            "s_number": 1
+        }
+
+        try:
+            # 发送POST请求
+            response = requests.post(url, headers=headers, data=data)
+            # 检查响应状态码,如果是200表示请求成功,可根据实际情况调整判断逻辑
+            if response.status_code == 200:
+                print("请求成功,响应内容如下:" + markdown_string)
+
+            else:
+                print(f"请求失败,状态码:{response.status_code}")
+        except requests.RequestException as e:
+            print(f"请求出现异常:{e}")
+
+    except requests.RequestException as e:
+        print(f"请求出现异常:{e}")
+
+
+if __name__ == "__main__":
+    save_to_showdoc('大模型/ai', 'deepseek', '1')

+ 49 - 0
utils/snowflake.py

@@ -0,0 +1,49 @@
+import time
+
+
+class Snowflake:
+    def __init__(self, datacenter_id, worker_id):
+        self.sequence = 0
+        self.last_timestamp = -1
+        self.datacenter_id = datacenter_id
+        self.worker_id = worker_id
+        self.DATACENTER_ID_BITS = 5
+        self.WORKER_ID_BITS = 5
+        self.SEQUENCE_BITS = 12
+        self.MAX_DATACENTER_ID = -1 ^ (-1 << self.DATACENTER_ID_BITS)
+        self.MAX_WORKER_ID = -1 ^ (-1 << self.WORKER_ID_BITS)
+        self.MAX_SEQUENCE = -1 ^ (-1 << self.SEQUENCE_BITS)
+        self.DATA_CENTER_SHIFT = self.WORKER_ID_BITS + self.SEQUENCE_BITS
+        self.WORKER_SHIFT = self.SEQUENCE_BITS
+        self.TIMESTAMP_LEFT_SHIFT = self.DATACENTER_ID_BITS + self.WORKER_ID_BITS + self.SEQUENCE_BITS
+
+    def generate_id(self):
+        timestamp = int(time.time() * 1000)
+        if timestamp < self.last_timestamp:
+            raise Exception("Clock moved backwards. Refusing to generate id")
+        if timestamp == self.last_timestamp:
+            self.sequence = (self.sequence + 1) & self.MAX_SEQUENCE
+            if self.sequence == 0:
+                timestamp = self.wait_next_millis(self.last_timestamp)
+        else:
+            self.sequence = 0
+        self.last_timestamp = timestamp
+        return (
+                (timestamp << self.TIMESTAMP_LEFT_SHIFT) |
+                (self.datacenter_id << self.DATA_CENTER_SHIFT) |
+                (self.worker_id << self.WORKER_SHIFT) |
+                self.sequence
+        )
+
+    def wait_next_millis(self, last_timestamp):
+        timestamp = int(time.time() * 1000)
+        while timestamp <= last_timestamp:
+            timestamp = int(time.time() * 1000)
+        return timestamp
+
+
+if __name__ == "__main__":
+    # 假设数据中心ID为1,工作机器ID为1
+    snowflake = Snowflake(231, 111)
+    for _ in range(10):
+        print(snowflake.generate_id())

+ 97 - 0
utils/test.py

@@ -0,0 +1,97 @@
+import os
+from datetime import datetime
+
+import fitz  # PyMuPDF
+from minio import Minio
+from minio.error import S3Error
+
+
+def pdf_to_images(pdf_path, output_folder, dpi=300):
+    """
+    将PDF每一页转换为图片
+    :param pdf_path: PDF文件路径
+    :param output_folder: 图片输出目录
+    :param dpi: 图片分辨率
+    :return: 生成的图片路径列表
+    """
+    # 创建输出目录
+    os.makedirs(output_folder, exist_ok=True)
+
+    # 打开PDF文件
+    pdf_document = fitz.open(pdf_path)
+    image_paths = []
+
+    # 遍历每一页
+    for page_num in range(len(pdf_document)):
+        page = pdf_document.load_page(page_num)
+
+        # 将页面转换为图片(使用RGB格式)
+        pix = page.get_pixmap(matrix=fitz.Matrix(dpi/72, dpi/72), dpi=dpi, colorspace="rgb")
+
+        # 生成图片文件名
+        image_name = f"page_{page_num+1}.png"
+        image_path = os.path.join(output_folder, image_name)
+
+        # 保存图片
+        pix.save(image_path)
+        image_paths.append(image_path)
+        print(f"已生成第 {page_num+1} 页图片: {image_path}")
+
+    pdf_document.close()
+    return image_paths
+
+def upload_to_minio(image_paths, bucket_name):
+    """
+    上传图片到Minio
+    :param image_paths: 图片路径列表
+    :param bucket_name: Minio存储桶名称
+    """
+    # 初始化Minio客户端
+    minio_client = Minio(
+        endpoint="localhost:9000",       # Minio服务器地址
+        access_key="your-access-key",    # 访问密钥
+        secret_key="your-secret-key",    # 私有密钥
+        secure=False                      # 是否使用HTTPS
+    )
+
+    # 确保存储桶存在
+    found = minio_client.bucket_exists(bucket_name)
+    if not found:
+        minio_client.make_bucket(bucket_name)
+        print(f"创建存储桶: {bucket_name}")
+
+    # 上传所有图片
+    for image_path in image_paths:
+        object_name = f"pdf_images/{datetime.now().strftime('%Y%m%d')}/{os.path.basename(image_path)}"
+
+        try:
+            minio_client.fput_object(
+                bucket_name=bucket_name,
+                object_name=object_name,
+                file_path=image_path,
+                content_type="image/png"
+            )
+            print(f"成功上传: {object_name}")
+        except S3Error as exc:
+            print(f"上传失败: {exc}")
+
+if __name__ == "__main__":
+    # 配置参数
+    PDF_PATH = "G:\\wx\\33333333.pdf"                # 输入PDF文件路径
+    OUTPUT_FOLDER = "G:\\wx\\temp_images"         # 临时图片存储目录
+    BUCKET_NAME = "documents"             # Minio存储桶名称
+
+    # 执行转换和上传
+    try:
+        # 步骤1: PDF转图片
+        images = pdf_to_images(PDF_PATH, OUTPUT_FOLDER)
+
+        # 步骤2: 上传到Minio
+        #upload_to_minio(images, BUCKET_NAME)
+
+    finally:
+        # 清理临时文件(可选)
+        # for img in images:
+        #     os.remove(img)
+        # os.rmdir(OUTPUT_FOLDER)
+        pass