girishwangikar commited on
Commit
4dd0399
·
verified ·
1 Parent(s): ca58cd2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -47
app.py CHANGED
@@ -42,7 +42,7 @@ def generate_response(
42
  message: str,
43
  history: list,
44
  system_prompt: str,
45
- temperature: float = 0.7,
46
  max_tokens: int = 512
47
  ):
48
  conversation = [
@@ -50,27 +50,24 @@ def generate_response(
50
  ]
51
  for prompt, answer in history:
52
  conversation.extend([
53
- {"role": "user", "content": str(prompt)},
54
- {"role": "assistant", "content": str(answer)},
55
  ])
56
- conversation.append({"role": "user", "content": str(message)})
57
 
58
- try:
59
- response = client.chat.completions.create(
60
- model="llama-3.1-8B-Instant",
61
- messages=conversation,
62
- temperature=temperature,
63
- max_tokens=max_tokens,
64
- stream=True
65
- )
66
 
67
- partial_message = ""
68
- for chunk in response:
69
- if chunk.choices[0].delta.content is not None:
70
- partial_message += chunk.choices[0].delta.content
71
- yield partial_message
72
- except Exception as e:
73
- yield f"An error occurred: {str(e)}"
74
 
75
  def clear_conversation():
76
  return [], None
@@ -83,12 +80,27 @@ with gr.Blocks(css=CSS, theme="GSW") as demo:
83
  fn=generate_response,
84
  chatbot=chatbot,
85
  fill_height=True,
86
- textbox=gr.Textbox(
87
- placeholder="Type a message",
88
- container=False,
89
- scale=7,
90
- lines=3
91
- ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  examples=[
93
  ["What are Python generators, and how do I use them effectively in my code?"],
94
  ["Can you explain the different types of SQL joins with examples?"],
@@ -100,28 +112,6 @@ with gr.Blocks(css=CSS, theme="GSW") as demo:
100
 
101
  clear_btn = gr.Button("Clear Conversation")
102
  clear_btn.click(clear_conversation, outputs=[chatbot, chatbot])
103
-
104
- with gr.Accordion("⚙️ Parameters", open=False):
105
- system_prompt = gr.Textbox(
106
- value="You are a helpful coding assistant, specialized in code completion, debugging, and analysis. Provide concise and accurate responses.",
107
- label="System Prompt",
108
- )
109
- temperature = gr.Slider(
110
- minimum=0,
111
- maximum=1,
112
- step=0.1,
113
- value=0.5,
114
- label="Temperature",
115
- )
116
- max_tokens = gr.Slider(
117
- minimum=50,
118
- maximum=1024,
119
- step=1,
120
- value=512,
121
- label="Max tokens",
122
- )
123
-
124
- chat_interface.additional_inputs = [system_prompt, temperature, max_tokens]
125
 
126
  gr.HTML(FOOTER_TEXT)
127
 
 
42
  message: str,
43
  history: list,
44
  system_prompt: str,
45
+ temperature: float = 0.5,
46
  max_tokens: int = 512
47
  ):
48
  conversation = [
 
50
  ]
51
  for prompt, answer in history:
52
  conversation.extend([
53
+ {"role": "user", "content": prompt},
54
+ {"role": "assistant", "content": answer},
55
  ])
56
+ conversation.append({"role": "user", "content": message})
57
 
58
+ response = client.chat.completions.create(
59
+ model="llama-3.1-8B-Instant",
60
+ messages=conversation,
61
+ temperature=temperature,
62
+ max_tokens=max_tokens,
63
+ stream=True
64
+ )
 
65
 
66
+ partial_message = ""
67
+ for chunk in response:
68
+ if chunk.choices[0].delta.content is not None:
69
+ partial_message += chunk.choices[0].delta.content
70
+ yield partial_message
 
 
71
 
72
  def clear_conversation():
73
  return [], None
 
80
  fn=generate_response,
81
  chatbot=chatbot,
82
  fill_height=True,
83
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False),
84
+ additional_inputs=[
85
+ gr.Textbox(
86
+ value="You are a helpful coding assistant, specialized in code completion, debugging, and analysis. Provide concise and accurate responses.",
87
+ label="System Prompt",
88
+ ),
89
+ gr.Slider(
90
+ minimum=0,
91
+ maximum=1,
92
+ step=0.1,
93
+ value=0.5,
94
+ label="Temperature",
95
+ ),
96
+ gr.Slider(
97
+ minimum=50,
98
+ maximum=1024,
99
+ step=1,
100
+ value=512,
101
+ label="Max tokens",
102
+ ),
103
+ ],
104
  examples=[
105
  ["What are Python generators, and how do I use them effectively in my code?"],
106
  ["Can you explain the different types of SQL joins with examples?"],
 
112
 
113
  clear_btn = gr.Button("Clear Conversation")
114
  clear_btn.click(clear_conversation, outputs=[chatbot, chatbot])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  gr.HTML(FOOTER_TEXT)
117