sijieaaa commited on
Commit
0988d38
·
verified ·
1 Parent(s): 6334316

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -5
app.py CHANGED
@@ -1,7 +1,103 @@
1
- import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SPDX-License-Identifier: Apache-2.0
2
+
3
+ import os
4
+ os.system('nvcc --version')
5
+ os.system('python --version')
6
+
7
+
8
+
9
+
10
+ model_url = 'http://localhost:8000/v1'
11
+ model = "sijieaaa/CodeModel-V1-3B-2024-02-07"
12
+ stop_token_ids = ''
13
+
14
+
15
+
16
+ import subprocess
17
+
18
+ # Setup LLM API
19
+ # -- llama-factory
20
+ def start_api():
21
+ # 以非阻塞方式启动 API
22
+ process = subprocess.Popen(
23
+ ["lmf", "api", "--model_name", "sijieaaa/CodeModel-V1-3B-2024-02-07", "--template", "qwen"],
24
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
25
+ )
26
+ # 监听 API 启动日志,检测关键字
27
+ while True:
28
+ line = process.stdout.readline()
29
+ if not line:
30
+ break # 没有新输出,继续等待
31
+ print(line.strip()) # 打印 API 启动日志
32
+ if "Running on local URL:" in line or "API started" in line or "http://localhost:" in line:
33
+ print("✅ API 启动成功!")
34
+ return process # 返回 API 进程
35
+ print("❌ API 启动失败!")
36
+ process.terminate()
37
+ return None
38
+ api_process = start_api()
39
+ # API 启动后执行下一步
40
+ if api_process:
41
+ print("🎯 执行后续操作……")
42
+ else:
43
+ raise Exception("API 启动失败!")
44
+
45
 
 
 
46
 
47
+
48
+
49
+ # Setup OpenAI API client
50
+ from openai import OpenAI
51
+ openai_api_key = '0'
52
+ openai_api_base = model_url
53
+ client = OpenAI(
54
+ api_key=openai_api_key,
55
+ base_url=openai_api_base,
56
+ )
57
+
58
+
59
+ def predict(message, history):
60
+ # Convert chat history to OpenAI format
61
+ history_openai_format = [{
62
+ "role": "system",
63
+ "content": "You are a great ai assistant."
64
+ }]
65
+ for human, assistant in history:
66
+ history_openai_format.append({"role": "user", "content": human})
67
+ history_openai_format.append({
68
+ "role": "assistant",
69
+ "content": assistant
70
+ })
71
+ history_openai_format.append({"role": "user", "content": message})
72
+
73
+ # Create a chat completion request and send it to the API server
74
+ stream = client.chat.completions.create(
75
+ model=model, # Model name to use
76
+ messages=history_openai_format, # Chat history
77
+ temperature=0.95, # Temperature for text generation
78
+ stream=True, # Stream response
79
+ top_p=0.7,
80
+ extra_body={
81
+ 'repetition_penalty':
82
+ 1,
83
+ 'stop_token_ids': [
84
+ int(id.strip()) for id in stop_token_ids.split(',')
85
+ if id.strip()
86
+ ] if stop_token_ids else []
87
+ })
88
+
89
+ # Read and return generated text from response stream
90
+ partial_message = ""
91
+ for chunk in stream:
92
+ partial_message += (chunk.choices[0].delta.content or "")
93
+ yield partial_message
94
+
95
+
96
+
97
+ import gradio as gr
98
+ # Create and launch a chat interface with Gradio
99
+ gr.ChatInterface(predict).queue().launch(
100
+ # server_name=host,
101
+ # server_port=port,
102
+ share=True
103
+ )