seawolf2357 commited on
Commit
f8eb332
·
verified ·
1 Parent(s): 0e0988e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -25
app.py CHANGED
@@ -5,7 +5,7 @@ import json
5
 
6
  ACCESS_TOKEN = os.getenv("HF_TOKEN")
7
 
8
- def respond(message, max_tokens=512, temperature=0.7, top_p=0.95):
9
  data = {
10
  "model": "command-r-plus:104b-fp16",
11
  "prompt": message,
@@ -13,40 +13,35 @@ def respond(message, max_tokens=512, temperature=0.7, top_p=0.95):
13
  "temperature": temperature,
14
  "top_p": top_p
15
  }
16
-
17
  # API 요청
18
- response = requests.post("http://hugpu.ai:7877/api/generate", json=data)
19
- try:
20
- # 개별 JSON 객체들을 분리하여 처리
21
- responses = []
22
- for obj in response.text.strip().split('\n'):
23
- # 각 줄을 JSON으로 파싱
24
- result = json.loads(obj)
25
- if result.get("done", False):
26
- break
27
- responses.append(result.get('response', ''))
28
-
29
- # 결과 텍스트를 조합
30
- generated_text = ''.join(responses)
31
- except json.JSONDecodeError as e:
32
- print("Failed to decode JSON from response:", e)
33
- generated_text = "An error occurred while processing your request."
34
 
35
- return generated_text
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- demo = gr.Interface(
38
  fn=respond,
39
- inputs=[
40
- gr.Textbox(label="Your Message", placeholder="Type your message here..."),
41
  gr.Slider(minimum=1, maximum=2048, value=512, label="Max Tokens"),
42
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, label="Temperature"),
43
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-P")
44
  ],
45
- outputs=gr.Textbox(),
46
  title="Advanced AI Chatbot",
47
- description="Enter your message and receive a response generated by an advanced AI model.",
48
  theme="Nymbo/Nymbo_Theme"
49
  )
50
 
51
  if __name__ == "__main__":
52
- demo.launch()
 
5
 
6
  ACCESS_TOKEN = os.getenv("HF_TOKEN")
7
 
8
+ def respond(message, history, max_tokens=512, temperature=0.7, top_p=0.95):
9
  data = {
10
  "model": "command-r-plus:104b-fp16",
11
  "prompt": message,
 
13
  "temperature": temperature,
14
  "top_p": top_p
15
  }
16
+
17
  # API 요청
18
+ response = requests.post("http://hugpu.ai:7877/api/generate", json=data, stream=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ partial_message = ""
21
+ for line in response.iter_lines():
22
+ if line:
23
+ try:
24
+ result = json.loads(line)
25
+ if result.get("done", False):
26
+ break
27
+ new_text = result.get('response', '')
28
+ partial_message += new_text
29
+ yield partial_message
30
+ except json.JSONDecodeError as e:
31
+ print(f"Failed to decode JSON: {e}")
32
+ yield "An error occurred while processing your request."
33
 
34
+ demo = gr.ChatInterface(
35
  fn=respond,
36
+ additional_inputs=[
 
37
  gr.Slider(minimum=1, maximum=2048, value=512, label="Max Tokens"),
38
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, label="Temperature"),
39
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, label="Top-P")
40
  ],
 
41
  title="Advanced AI Chatbot",
42
+ description="Enter your message and receive a streaming response generated by an advanced AI model.",
43
  theme="Nymbo/Nymbo_Theme"
44
  )
45
 
46
  if __name__ == "__main__":
47
+ demo.launch()