csabakecskemeti commited on
Commit
71d0a2d
·
verified ·
1 Parent(s): 996b1d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -45
app.py CHANGED
@@ -3,89 +3,85 @@ import requests, json
3
 
4
  public_ip = '71.202.66.108'
5
 
6
- model = 'llama3.1:latest' #You can replace the model name if needed
7
  context = []
8
 
9
-
10
- import gradio as gr
11
-
12
- # ollama_serve = f"http://{mac_pro_ip}:11434/api/generate"
13
  ollama_serve = f"http://{public_ip}:11434/api/generate"
14
 
15
- #Call Ollama API
16
  def generate(prompt, context, top_k, top_p, temp):
17
  r = requests.post(ollama_serve,
18
- json={
19
- 'model': model,
20
- 'prompt': prompt,
21
- 'context': context,
22
- 'options':{
23
- 'top_k': top_k,
24
- 'temperature':top_p,
25
- 'top_p': temp
26
- }
27
- },
28
- stream=True)
29
  r.raise_for_status()
30
 
31
-
32
  response = ""
33
 
34
  for line in r.iter_lines():
35
  body = json.loads(line)
36
  response_part = body.get('response', '')
37
- print(response_part)
38
  if 'error' in body:
39
- raise Exception(body['error'])
 
40
 
41
- response += response_part
 
 
 
42
 
43
  if body.get('done', False):
44
  context = body.get('context', [])
45
- return response, context
46
-
47
-
48
 
49
  def chat(input, chat_history, top_k, top_p, temp):
50
-
51
  chat_history = chat_history or []
52
-
53
  global context
54
- output, context = generate(input, context, top_k, top_p, temp)
 
 
 
55
 
56
- chat_history.append((input, output))
 
57
 
58
- return chat_history, chat_history
59
- #the first history in return history, history is meant to update the
60
- #chatbot widget, and the second history is meant to update the state
61
- #(which is used to maintain conversation history across interactions)
 
62
 
63
 
64
- #########################Gradio Code##########################
65
  block = gr.Blocks()
66
 
67
-
68
  with block:
69
 
70
- gr.Markdown("""<h1><center> Trashcan AI </center></h1>
71
- """)
72
-
73
- gr.Markdown("""<h3><center> LLama3.1 hosted on a 2013 "Trashcan" Mac Pro with ollama </center></h3>
74
- """)
75
 
76
  chatbot = gr.Chatbot()
77
  message = gr.Textbox(placeholder="Type here")
78
 
79
  state = gr.State()
80
  with gr.Row():
81
- top_k = gr.Slider(0.0,100.0, label="top_k", value=40, info="Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more diverse answers, while a lower value (e.g. 10) will be more conservative. (Default: 40)")
82
- top_p = gr.Slider(0.0,1.0, label="top_p", value=0.9, info=" Works together with top-k. A higher value (e.g., 0.95) will lead to more diverse text, while a lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.9)")
83
- temp = gr.Slider(0.0,2.0, label="temperature", value=0.8, info="The temperature of the model. Increasing the temperature will make the model answer more creatively. (Default: 0.8)")
84
-
85
 
86
  submit = gr.Button("SEND")
87
-
 
88
  submit.click(chat, inputs=[message, state, top_k, top_p, temp], outputs=[chatbot, state])
89
 
90
  if __name__ == "__main__":
91
- block.launch()
 
3
 
4
  public_ip = '71.202.66.108'
5
 
6
+ model = 'llama3.1:latest' # You can replace the model name if needed
7
  context = []
8
 
 
 
 
 
9
  ollama_serve = f"http://{public_ip}:11434/api/generate"
10
 
11
+ # Call Ollama API
12
  def generate(prompt, context, top_k, top_p, temp):
13
  r = requests.post(ollama_serve,
14
+ json={
15
+ 'model': model,
16
+ 'prompt': prompt,
17
+ 'context': context,
18
+ 'options': {
19
+ 'top_k': top_k,
20
+ 'temperature': top_p,
21
+ 'top_p': temp
22
+ }
23
+ },
24
+ stream=True)
25
  r.raise_for_status()
26
 
 
27
  response = ""
28
 
29
  for line in r.iter_lines():
30
  body = json.loads(line)
31
  response_part = body.get('response', '')
32
+
33
  if 'error' in body:
34
+ yield f"Error: {body['error']}"
35
+ return
36
 
37
+ # Append token to the growing response and yield the entire response so far
38
+ if response_part:
39
+ response += response_part
40
+ yield response # Yield the growing response incrementally
41
 
42
  if body.get('done', False):
43
  context = body.get('context', [])
44
+ return # End the generator once done
 
 
45
 
46
  def chat(input, chat_history, top_k, top_p, temp):
 
47
  chat_history = chat_history or []
 
48
  global context
49
+
50
+ # Initialize the user input as part of the chat history
51
+ chat_history.append((input, "")) # Add user input first
52
+ response = "" # Initialize empty response
53
 
54
+ # Stream each part of the response as it's received
55
+ response_stream = generate(input, context, top_k, top_p, temp)
56
 
57
+ for response_part in response_stream:
58
+ response = response_part # Keep updating with the new part of the response
59
+ # Update the latest assistant response (the second part of the tuple)
60
+ chat_history[-1] = (input, response)
61
+ yield chat_history, chat_history # Yield the updated chat history
62
 
63
 
64
+ ######################### Gradio Code ##########################
65
  block = gr.Blocks()
66
 
 
67
  with block:
68
 
69
+ gr.Markdown("""<h1><center> Trashcan AI </center></h1>""")
70
+ gr.Markdown("""<h3><center> LLama3.1 hosted on a 2013 "Trashcan" Mac Pro with ollama </center></h3>""")
 
 
 
71
 
72
  chatbot = gr.Chatbot()
73
  message = gr.Textbox(placeholder="Type here")
74
 
75
  state = gr.State()
76
  with gr.Row():
77
+ top_k = gr.Slider(0.0, 100.0, label="top_k", value=40, info="Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more diverse answers, while a lower value (e.g. 10) will be more conservative. (Default: 40)")
78
+ top_p = gr.Slider(0.0, 1.0, label="top_p", value=0.9, info="Works together with top-k. A higher value (e.g., 0.95) will lead to more diverse text, while a lower value (e.g., 0.5) will generate more focused and conservative text. (Default: 0.9)")
79
+ temp = gr.Slider(0.0, 2.0, label="temperature", value=0.8, info="The temperature of the model. Increasing the temperature will make the model answer more creatively. (Default: 0.8)")
 
80
 
81
  submit = gr.Button("SEND")
82
+
83
+ # Use .click() to trigger the response streaming
84
  submit.click(chat, inputs=[message, state, top_k, top_p, temp], outputs=[chatbot, state])
85
 
86
  if __name__ == "__main__":
87
+ block.launch()