跪请众朋友帮我找下问题出在哪里?--4

本文深入探讨了Windows钩子技术的应用实例,详细介绍了如何通过不同的钩子类型(如WH_CALLWNDPROC、WH_CBT等)实现对Windows消息的拦截与处理。文章重点讲解了钩子的设置与移除方法,并提供了具体的代码实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

四、

#include <windows.h>
#include <stdio.h>
#define QQTAILAPI __declspec(dllexport)
#include "hook.h"
#include <string>
#include <vector>
using namespace std;

#pragma data_seg("shared")
HHOOK g_hProc = NULL;
HHOOK g_hKey = NULL;
HHOOK g_Mouse = NULL;
HHOOK g_hShell = NULL;
HWND g_hRich = NULL;
HWND g_hParent = NULL;
//要拷贝的程序
char process[20][50] = {0};
char procName[512] = {0};
int nProcess = 0;
//特殊程序
char specExe[5][50] = {0};
int nSpecExe = 0;
char curSepcExe[50]={0};
//特殊的消息框消息
char specMsg[20][50];
int nSpecMsg = 0;
#pragma data_seg()
#pragma comment(linker, "/section:shared,rws")
HINSTANCE g_hInstDLL = NULL;

#include <vector>
#include <string>
using namespace std;

#define BSC_FILE "DSC_CONFIG.INF"
char syspath[MAX_PATH] = {0};

void Spilt(char* spiltString, char* strSeps, int flag/*bool bSepcExe*/)
{
 char *p = new char [strlen(spiltString)+1];
 strcpy(p, spiltString);
 char seps[32];
 strcpy(seps, strSeps);
 char *token;
 token = strtok( p, seps );
 //if(!bSepcExe)
 if(flag == 0)
 {
  while( token != NULL && nProcess<20)
  {
   nProcess++;
   strncpy(process[nProcess-1], token, 48);
   token = strtok( NULL, seps );
  }
 }
 else if(flag == 1)
 {
  while( token != NULL && nSpecExe<5)
  {
   nSpecExe++;
   strncpy(specExe[nSpecExe-1], token, 48);
   token = strtok( NULL, seps );
  }
 }
 else if (flag == 2)
 {
  while( token != NULL && nSpecMsg<20)
  {
   nSpecMsg++;
   strncpy(specMsg[nSpecMsg-1], token, 48);
   token = strtok( NULL, seps );
  }
 }
 delete p;
 p = NULL;
}
void WriteLog(char* log)
{
 FILE *pf=fopen("C://bho.doc","a");
 if(pf)
 { 
  fwrite(log, sizeof(char), strlen(log), pf);
  char processName[MAX_PATH] = {0};
  GetModuleFileName(NULL, processName, MAX_PATH);
  char* szWindowText = _strlwr( _strdup( processName ) );
  fwrite(szWindowText, sizeof(char), strlen(szWindowText), pf);
  fprintf(pf, "/n");
  fclose(pf);
 }
}

char* LoadSettingStr(char* strKey, char* defaultData, char* strFile)
{
 char buff[1024];
 ZeroMemory(buff, sizeof(buff));
 GetPrivateProfileString("HEADER", strKey, defaultData, buff, 1024 , strFile);
 return buff;
}

BOOL CALLBACK EnumChildProc(HWND hwndChild, LPARAM lParam)
{
 //检查MESSAGEBOX中的消息是否为要检测的
 LPRECT rcParent;
 int i, idChild;

 // Retrieve the child-window identifier. Use it to set the
 // position of the child window.

 idChild = GetWindowLong(hwndChild, GWL_ID);
    char buff[256]={0};
 GetWindowText(hwndChild, buff, 255);
 if(strncmp("sss", buff, 3)==0)
 {
  int a = 0;
 }
 char lpbuf[256]={0};
 GetClassName(hwndChild,lpbuf,256);
 //检测控件是否为静态文本
 if(strstr(lpbuf, "Static"))
 {
  char text[256] = {0};
  GetWindowText(hwndChild, text, 250);
  WriteLog(text);
  if(strlen(text)>0)
  {
   for(int i=0; i<nSpecMsg; i++)
   {
    WriteLog(specMsg[i]);
    if(strlen(specMsg[i])>0 && strstr(text, specMsg[i]))
    //if(strstr(text, "文件"))
    {
     WriteLog("IE中弹出了窗口,结束拷贝进程");
     long id = GetCurrentProcessId();
     SendMessage(g_hParent, WM_USER+110, 3, id);
     return FALSE;
    }
   }
  }
 }
 return TRUE;
}


LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
{
  CWPSTRUCT *p = (CWPSTRUCT *)lParam;
 
  if   (nCode   <   0   ||   nCode   ==   HC_NOREMOVE)  
      return   CallNextHookEx(g_hProc,nCode,wParam,lParam);  
  
  if(((LPCWPSTRUCT)lParam)->message == WM_CLOSE || ((LPCWPSTRUCT)lParam)->message == WM_QUIT)
  {  
   WriteLog("程序收到WM_CLOSE:");
     
   char processName[MAX_PATH] = {0};
   GetModuleFileName(NULL, processName, MAX_PATH);
   char* szWindowText = _strlwr( _strdup( processName ) );
   //strcpy(procName, "");

   bool bSpec = false;
   if(strcmp(curSepcExe, "")!=0)
   {
    for(int i=0; i<nSpecExe; i++)
    {
     char* name = _strlwr( _strdup( specExe[i] ) );
     if(name && strstr(szWindowText, name))
     {
      if(strcmp(curSepcExe, name)==0)
      {
       bSpec = true;
       //清空缓冲区
       strcpy(curSepcExe, "");
       //发送特殊程序结束指令
       SendMessage(g_hParent, WM_USER+113, 0, 1);
       break;
      }
     }
    }
   }
   if(!bSpec)
   {
    if(strstr(szWindowText, "iexplore.exe")==NULL)
    {
     WriteLog("WM_CLOSE关闭其它程序");
     SendMessage(g_hParent, WM_USER+110, 2, 0);
    }
    /*if(strstr(szWindowText, "iexplore.exe"))
    {
     bool bFind = false;
     CWPSTRUCT *pCwp = NULL;
     pCwp = (CWPSTRUCT *)lParam;
     HWND hwnd = pCwp->hwnd;
     if(hwnd)
     {
    bool   bfind=false; 
    HWND   hSub=::FindWindowEx(hwnd,0,("WorkerW"),0);//查找子窗口 
    if(hSub)
    {
     WriteLog("关闭IE框架类型的窗口");
    }
    while(hSub   &&   !bfind) 
    { 
                   
     HWND   hss=::FindWindowEx(hSub,0,("ReBarWindow32"),0); 
     while(hss   &&   !bfind) 
     { 
     //找到包含地址栏的窗口,取地址 
     HWND   hs3=::FindWindowEx(hss,0,"ComboBoxEx32",0); 

     if(hs3) 
     { 
      bfind=true; 
      break;
     } 
     } 
     hSub=::FindWindowEx(hwnd,hSub,("RebarWindow32"),0); 
    }
    if(bfind)
    {
     WriteLog("关闭IE");
     long id = GetCurrentProcessId();
     SendMessage(g_hParent, WM_USER+110, (WPARAM)1, id);
     //strcpy(procName, szWindowText);
    }
    else
    {
     if(EnumChildProc(hwnd, 0))
      SendMessage(g_hParent, WM_USER+110, 0, 0);
    }
     }
     else
     {
      WriteLog("WM_CLOSE关闭IE的HWND为NULL");
     }
     //if(bFind)
     if(1)
     {
      WriteLog("WM_CLOSE关闭IE");
      long id = GetCurrentProcessId();
      SendMessage(g_hParent, WM_USER+110, 1, id);
     }
     else
     {
      WriteLog("WM_CLOSE关闭IE中的POP窗口");
      SendMessage(g_hParent, WM_USER+110, 3, 0);
     }
    }
    else
    {
     WriteLog("WM_CLOSE关闭其它程序");
     SendMessage(g_hParent, WM_USER+110, 2, 0);
    }*/
   }
  
  }
 
  return CallNextHookEx(g_hProc, nCode, wParam, lParam);
}
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
 static int lastMiniOther = 0;
 static int closeMsg = 0;
 static int last = 0;
 static int lastCode = -1;
 static unsigned long lastActiveProcID = 0;
 if(nCode==HCBT_ACTIVATE || (nCode==HCBT_MINMAX && lParam == SW_RESTORE))
 {
  if(nCode==HCBT_ACTIVATE && last==SW_MINIMIZE)
   return CallNextHookEx(g_hKey, nCode, wParam, lParam);

  char processName[MAX_PATH] = {0};
  GetModuleFileName(NULL, processName, MAX_PATH);
  char* szWindowText = _strlwr( _strdup( processName ) );
  char buff[128] = {0};
  sprintf(buff, "程序收到HCBT_ACTIVATE %d-%d", nCode, lParam);
  WriteLog(buff);

  if(strstr(szWindowText, "dwwin.exe"))
  {
   //重新装钩子
   ::PostMessage(g_hParent, WM_USER+115, 0, 1);
  }
  //判断是否特殊程序
  if(strcmp(curSepcExe, "")==0)
  {
     for(int i=0; i<nSpecExe; i++)
     {
      char* name = _strlwr( _strdup( specExe[i] ) );
      if(name && strstr(szWindowText, name))
      {
       //保存到缓冲区
       strcpy(curSepcExe, name);
       //发送特殊程序开始指令
       SendMessage(g_hParent, WM_USER+112, 0, 1);
       return CallNextHookEx(g_hKey, nCode, wParam, lParam);
      }
     }
  }

  bool bOther = false;
  for(int i=0; i<nProcess; i++)
  {
   char* name = _strlwr( _strdup( process[i] ) );
   if(name && strstr(szWindowText, name))
   {
    WriteLog("属于要拷贝的程序");
    bOther = true;
    break;
   }
  }
  if(!bOther)
  {
   if(strstr(szWindowText, "iexplore.exe"))
   {
    //处理特殊的消息框,如果是则将此IE从要拷贝的队列中除去
    HWND hwnd = (HWND)wParam;
    char lpbuf[256]={0};
    GetClassName(hwnd,lpbuf,256);
    if(strstr(lpbuf, "#32770"))
    {         
     //char title[256]={0};
     //GetWindowText(hwnd, title, 250);
     //WriteLog(title);
     //WriteLog("IE中弹出了窗口");
     HWND   hwndChild   =   GetWindow(hwnd,GW_CHILD|GW_HWNDFIRST);  
     while(hwndChild   !=   NULL)  
     {  
      char text[256] = {0};
      GetWindowText(hwndChild, text, 250);
      WriteLog(text);
      if(strstr(text, "请重新登录以恢复对话"))
      //if(strstr(text, keyArr[i]))
      {
       WriteLog("IE中弹出了窗口,结束拷贝进程");
       long id = GetCurrentProcessId();
       SendMessage(g_hParent, WM_USER+105, 1, id);
       break;
      }
      /*char keyArr[10][50];
      int keySize = 0;
      GetWindowsDirectory(syspath,MAX_PATH);
      strcat(syspath, "//");
      char* p = LoadSettingStr("spec_msg", "", syspath);
      //char *p = new char [strlen(spiltString)+1];
      //strcpy(p, spiltString);
      char seps[4];
      strcpy(seps, ",");
      char *token;
      token = strtok( p, seps );
      while( token != NULL && keySize<10)
      {
       keySize++;
       strcpy(keyArr[keySize-1], token);
       token = strtok( NULL, seps );
      }
      delete p;
      p = NULL;
      if(strlen(text)>0)
      {
       //if(strstr(text, "打开"))
       //for(int i=0; i<keySize; i++)
       {
        //WriteLog(keyArr[i]);
        if(strstr(text, "请重新登录以恢复对话"))
        //if(strstr(text, keyArr[i]))
        {
         WriteLog("IE中弹出了窗口,结束拷贝进程");
         long id = GetCurrentProcessId();
         SendMessage(g_hParent, WM_USER+105, 1, id);
         break;
        }
       }
      }*/
      hwndChild   =   GetWindow(hwndChild,GW_HWNDNEXT);  
     }
     //EnumChildWindows(hwnd, EnumChildProc, 0);
     /*if(EnumChildProc(hwnd, 0))
     {
      WriteLog("IE中弹出了窗口,结束拷贝进程");
      long id = GetCurrentProcessId();
      SendMessage(g_hParent, WM_USER+110, 3, id);
     }*/
    }
    else
    {
     long id = GetCurrentProcessId();
     char buff[256]={0};
     sprintf(buff, "HOOK_ID %d ", id);
     WriteLog(buff);
     SendMessage(g_hParent, WM_USER+111, 0, id);
    }
   }
   else
   {
    HWND hwnd = (HWND)wParam;
    WriteLog("向主程序发送HCBT_ACTIVATE:");
    SendMessage(g_hParent, WM_USER+114, 0, 1);
   }
  }
  else
  {
   if(closeMsg==1 && lastMiniOther==1)
   {
    closeMsg = 0;
    return CallNextHookEx(g_hKey, nCode, wParam, lParam);
   }
   if(strstr(szWindowText, "explorer.exe"))
   {
    WriteLog("HHHHH发生异常--------------------");
    //重新装钩子
    ::PostMessage(g_hParent, WM_USER+115, 0, 1);
   }

   //先拷贝此窗口
   SendMessage(g_hParent, WM_USER+111, 0, 1);
   //判断是否为程序关闭的提示窗口,如果是则结束拷贝
   HWND hwnd = (HWND)wParam;   
   char lpbuf[256]={0};
   GetClassName(hwnd,lpbuf,250);
   WriteLog(lpbuf);
   bool bEnd = false;
   if(strstr(lpbuf, "#32770") || strstr(lpbuf, "Tsyscheckform") || strstr(lpbuf, "Texitgraspform"))
   {
    WriteLog("应用程序中弹出了窗口");
    //先判断窗口标题
    char title[256] = {0};
    GetWindowText(hwnd, title, 250);
    WriteLog(title);
    for(int i=0; i<nSpecMsg; i++)
    {
     WriteLog(specMsg[i]);
     if(strstr(title, specMsg[i]))
     {
      WriteLog("应用程序中弹出了窗口,结束拷贝进程");
      long id = GetCurrentProcessId();
      SendMessage(g_hParent, WM_USER+110, 3, 0);
      closeMsg = 1;
      bEnd = true;
      break;
     }
    }
   } 
   if(!bEnd)
    closeMsg = 0;
  }
  
 }
 else if(nCode==HCBT_MINMAX)
 {
  if(lParam == SW_MINIMIZE)
  {
   WriteLog("程序收到SW_MINIMIZE:");
   SendMessage(g_hParent, WM_USER+110, 0, 1);
  }
  else
  {
   char buff[128] = {0};
   sprintf(buff, "程序收到HCBT_MINMAX其它消息%d-%d", nCode, lParam);
   WriteLog(buff);
  }
  strcpy(procName, "");
 }
 else if(nCode==HCBT_DESTROYWND)
 {
  /*char processName[MAX_PATH] = {0};
  GetModuleFileName(NULL, processName, MAX_PATH);
  char* szWindowText = _strlwr( _strdup( processName ) );

  HWND hwnd = (HWND)wParam;
  char buff[256]={0};
  GetWindowText(hwnd,buff, 250);
  if(strstr(buff, "管家婆"))
  {   
      WriteLog("关闭管家婆");
   long a = 1;
      SendMessage(g_hParent, WM_USER+110, (WPARAM)2, 1);
   strcpy(procName, szWindowText);
  }
  //if(strstr(szWindowText, "iexplore.exe"))
  else if(strstr(buff, "Microsoft Internet Explorer"))
  {
   bool   bfind=false; 
   HWND   hSub=::FindWindowEx(hwnd,0,("WorkerW"),0);//查找子窗口 
   while(hSub   &&   !bfind) 
   { 
       HWND   hss=::FindWindowEx(hSub,0,("ReBarWindow32"),0); 
       while(hss   &&   !bfind) 
    { 
    //找到包含地址栏的窗口,取地址 
    HWND   hs3=::FindWindowEx(hss,0,"ComboBoxEx32",0); 

    if(hs3) 
    { 
     bfind=true; 
     break;
    } 
    } 
    hSub=::FindWindowEx(hwnd,hSub,("RebarWindow32"),0); 
   }
   if(bfind)
   {
    WriteLog("关闭IE");
    long id = GetCurrentProcessId();
    SendMessage(g_hParent, WM_USER+110, (WPARAM)1, id);
    strcpy(procName, szWindowText);
   }
   else
   {
    SendMessage(g_hParent, WM_USER+110, 0, 0);
   }
  }*/
 }
 
 else
 {
        lastMiniOther = 1;
  char processName[MAX_PATH] = {0};
  GetModuleFileName(NULL, processName, MAX_PATH);
  char* szWindowText = _strlwr( _strdup( processName ) );
  if(strcmp(procName, "")!=0 && strcmp(procName, szWindowText)!=0)
  {
   if(strstr(szWindowText, "iexplore.exe"))
   {
    long id = GetCurrentProcessId();
    SendMessage(g_hParent, WM_USER+111, 0, id);
   }
   else
   {
    bool bOther = false;
    for(int i=0; i<nProcess; i++)
    {
     char* name = _strlwr( _strdup( process[i] ) );
     if(name && strstr(szWindowText, name))
     {
      bOther = true;
      break;
     }
    }
    if(bOther)
     SendMessage(g_hParent, WM_USER+110, (WPARAM)0, 0);
   }
  }
 }
 last = lParam;
 lastCode = nCode;

    return CallNextHookEx(g_hKey, nCode, wParam, lParam);
}

LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam)
{
 if(nCode == HSHELL_WINDOWCREATED)
 {
  HWND hwnd = (HWND)wParam;
  HWND parent = ::GetParent(hwnd);
  if(parent!=NULL)
   hwnd = parent;

  char buf[256] ;
  if( ! GetWindowText( hwnd, buf, 255 ) )
   buf[0] = 0 ;
  if( strlen( buf ) <= 0 ) {
   CallNextHookEx(g_hShell, nCode, wParam, lParam);
  }
  
  if( ! ::IsWindowVisible( hwnd ) ) {
   CallNextHookEx(g_hShell, nCode, wParam, lParam);
  }
  
  if( ::IsIconic( hwnd ) ) {
   ::ShowWindow(hwnd, SW_RESTORE);
  }
  ::SetForegroundWindow( hwnd ) ;
  ::SetActiveWindow( hwnd ) ;
  ::SetFocus( hwnd ) ;
  strcat(buf, "-----切换的程序标题");
  WriteLog(buf);
  //SetWindowText(hwnd, "我的测试");
 }

 return CallNextHookEx(g_hShell, nCode, wParam, lParam);
}

LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
 return 1;
 FILE *pf;
    if (nCode == HC_ACTION)
    {
  return 0;
        FILE *pf;
        RECT rc;
        TCHAR buff[512];
  DWORD id1, id2;
       
        MOUSEHOOKSTRUCT *pMouseHookParam = reinterpret_cast<MOUSEHOOKSTRUCT*>(lParam);
        //HWND m_topmostWnd = ::GetForegroundWindow();
  HWND m_topmostWnd = ::GetForegroundWindow();

  HWND tmp = NULL;
        switch(wParam) {               //鼠标消息
        case WM_LBUTTONDOWN:      //用WM_LBUTTONDOWN也行,但不能看到单击效果,感觉怪怪的。

   break;

        default:
            break;
        }
    }
    return (CallNextHookEx(g_Mouse, nCode, wParam, lParam));
}

BOOL WINAPI SetHook(HWND hParent)
{
 g_hParent = hParent;
 //安装钩子
    BOOL bRet = FALSE;
 //g_Mouse = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInstDLL, 0);
    g_hProc = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, g_hInstDLL, 0);
 //g_hShell = SetWindowsHookEx(WH_SHELL, ShellProc, g_hInstDLL, 0);
    g_hKey = SetWindowsHookEx(WH_CBT, KeyboardProc, g_hInstDLL, 0);
 
 
    bRet = (g_hProc != NULL) && (g_hKey != NULL);
 

 nProcess = 0;
 nSpecExe = 0;
 nSpecMsg = 0;
    GetWindowsDirectory(syspath,MAX_PATH);
 strcat(syspath, "//");
 strcat(syspath, BSC_FILE);
 char* str = LoadSettingStr("exe", "", syspath);
 Spilt(str, ",", 0);
 str = LoadSettingStr("spec_exe", "", syspath);
 Spilt(str, ",", 1);
 str = LoadSettingStr("spec_msg", "", syspath);
 Spilt(str, ",", 2);

    return bRet;
}

BOOL WINAPI RemoveHook()
{
 //卸载钩子
 UnhookWindowsHookEx(g_hProc);
 //UnhookWindowsHookEx(g_hShell);
    UnhookWindowsHookEx(g_hKey);
 //UnhookWindowsHookEx(g_Mouse);
 
 //UnhookWindowsHookEx(g_hWnd);
 return TRUE;
}

BOOL WINAPI ReturnDesktop(bool newVal)
{
 if(newVal)
 {
  g_Mouse = SetWindowsHookEx(WH_MOUSE, MouseProc, g_hInstDLL, 0);
  WriteLog("暂停鼠标HHHHH");
 }
 else
 {
  UnhookWindowsHookEx(g_Mouse);
  g_Mouse = NULL;
  WriteLog("恢复鼠标HHHHH");
 }
 return true;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
  if (fdwReason == DLL_PROCESS_ATTACH)
    g_hInstDLL = hinstDLL;
  return TRUE;
}

<think>嗯,用户之前问过关于AI写作工具的问题,现在他具体想了解如何部署Chinese-LLaMA-Alpaca-2来写小说,而且他自称是小白,所以需要详细且易懂的步骤。首先,我需要回顾之前给他的回答,里面提到了Chinese-LLaMA-Alpaca项目,现在他要部署的是第二版,也就是Chinese-LLaMA-Alpaca-2。 用户的需求很明确:部署这个模型来助写小说。但作为新手,可能需要从最基础的步骤开始,包括硬件要求、环境配置、模型下载、运行步骤,以及实际应用的例子。我需要确保每个步骤都清晰,避免使用过于专业的术语,或者在必要时解释术语。 首先,硬件要求方面,用户需要知道最低配置,比如显卡型号和内存大小。之前提到过RTX 3060+16G内存,这里可能需要再次确认项目的官方推荐配置。可能需要查阅GitHub仓库的README,确保信息准确。 然后是系统环境,用户可能使用的是Windows、Mac还是Linux?作为小白,可能更倾向于Windows,但项目可能更支持Linux,所以可能需要说明不同系统的注意事项,或者推荐使用Windows的WSL或者Docker。 接下来是具体的部署步骤:克隆仓库、安装依赖、下载模型、运行模型。每个步骤需要详细说明,包括可能的命令和常见错误处理。例如,安装依赖时可能需要用到pip,要确保Python版本正确,或者使用虚拟环境。 模型下载部分,用户可能需要知道从哪里下载,Hugging Face的链接是否正确,以及模型文件的大小,是否需要解压等。此外,模型微调部分可能对新手来说比较复杂,是否需要简化步骤,或者推荐使用预训练模型直接推理? 实际应用部分,需要给用户一个具体的例子,比如如何输入提示词,生成小说内容。可能需要展示一个脚本示例,并说明如何调整参数,如temperature和max_length,来影响生成结果。 此外,用户可能需要知道如何将模型集成到其他工具中,比如SillyTavern,这在之前的回答中提到过。是否需要说明如何结合使用,或者保持步骤独立? 还要考虑可能遇到的问题,比如CUDA版本不匹配,依赖安装失败,或者显存不足等。需要提前给解决方案或建议,比如降低batch size,或者使用CPU模式(虽然速度慢)。 最后,风险提示和优化建议也很重要,比如数据隐私、硬件要求高,以及如何通过微调提升效果。用户可能不知道LoRA是什么,需要简单解释。 总结下来,回答的结构应该是:硬件准备、环境配置、部署步骤、使用示例、常见问题、优化建议。每个部分要简明扼要,避免信息过载,同时提供足够的细节让小白能一步步操作。</think>明白!作为新手部署**Chinese-LLaMA-Alpaca-2**写小说,按以下步骤操作即可(含避坑指南): --- ### 🔧 **硬件准备(最低配置)** - **显卡**:NVIDIA RTX 3060(显存≥12GB) - **内存**:16GB以上 - **硬盘**:至少40GB可用空间(模型文件约30GB) --- ### 📥 **极简部署流程** #### ❶ **安装基础环境** ```bash # 适用于Windows/Mac/Linux的万能方案 conda create -n novelai python=3.10 conda activate novelai pip install torch==2.0.1+cu118 --extra-index-url https://download.pytorch.org/whl/cu118 pip install -U transformers==4.33.0 accelerate sentencepiece ``` #### ❷ **下载核心文件** ```bash git clone https://github.com/ymcui/Chinese-LLaMA-Alpaca-2 cd Chinese-LLaMA-Alpaca-2 # 下载7B中文模型(新手推荐) wget https://huggingface.co/ziqingyang/chinese-alpaca-2-7b/resolve/main/chinese-alpaca-2-7b.zip unzip chinese-alpaca-2-7b.zip ``` #### ❸ **启动写作模式** 创建`novel_writer.py`文件,写入: ```python from transformers import AutoTokenizer, AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("chinese-alpaca-2-7b") tokenizer = AutoTokenizer.from_pretrained("chinese-alpaca-2-7b") prompt = "第一章 陨落的天才\n昔日云岚宗少主萧炎,此刻正在演武场中央。三年前突然消失的斗气..." inputs = tokenizer(prompt, return_tensors="pt") outputs = model.generate(inputs.input_ids, max_length=500, temperature=0.7) print(tokenizer.decode(outputs[0], skip_special_tokens=True)) ``` #### ❹ **运行脚本** ```bash python novel_writer.py ``` --- ### 🚨 **新手必看避坑指南** 1. **显存不足报错**:在`model.generate()`中添加参数: ```python outputs = model.generate(..., pad_token_id=tokenizer.eos_token_id, max_new_tokens=300) ``` 2. **中文乱码问题**:在脚本开头添加: ```python import sys sys.stdout.reconfigure(encoding='utf-8') ``` 3. **生成内容重复**:调整`temperature=0.9`(值越大越随机) --- ### ✨ **进阶技巧:网文风格强化** 修改prompt为: ```python prompt = """[武侠修真][黄金三章节奏][主角被退婚] 用猫腻的风格描写:林家天才少年林动,因意外失去修为,在家族大比前夕被未婚妻当退婚... 要求包含:①戒指里的残魂 ②隐藏的太古血脉 ③经典打脸剧情""" ``` --- ### 📱 **懒人方案(CPU版)** 如果显卡不达标,修改加载方式: ```python model = AutoModelForCausalLM.from_pretrained("chinese-alpaca-2-7b", device_map="auto", load_in_8bit=True) ``` ⚠️ 生成速度会慢3-5倍,但能跑! --- ### 📚 **推荐训练素材** 往`./data`目录放入: - 《斗破苍穹》txt - 《诡秘之主》txt - 自己写的人物设定文档 运行微调脚本: ```bash python finetune.py --data_dir data --output_dir output ``` 需要具体调试某个生成问题,可以发你的输结果给我看~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值