Bhaskar2611 commited on
Commit
1b42ae7
·
verified ·
1 Parent(s): 43baf7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -13
app.py CHANGED
@@ -63,44 +63,79 @@ import gradio as gr
63
  from dotenv import load_dotenv
64
  from huggingface_hub import InferenceClient
65
 
 
66
  load_dotenv()
67
 
68
  HF_TOKEN = os.getenv("HF_TOKEN")
 
69
  if not HF_TOKEN:
70
  raise ValueError("HF_TOKEN is missing.")
71
 
 
 
 
 
 
 
 
72
  system_message = (
73
  "You are a helpful and experienced coding assistant specialized in web development. "
74
  "Help the user by generating complete and functional code for building websites. "
75
- "You can provide HTML, CSS, JavaScript, and backend code (like Flask, Node.js, etc.) "
76
  "based on their requirements."
77
  )
78
 
79
- client = InferenceClient(
80
- provider="nscale",
81
- api_key=HF_TOKEN,
82
- )
83
-
84
  def chat_function(message, history):
85
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
86
 
87
  history = history or []
 
88
  for item in history:
 
 
89
  if isinstance(item, dict):
 
90
  role = item.get("role")
91
  content = item.get("content", "")
92
- if role in ("user", "assistant"):
93
- messages.append({"role": role, "content": content})
 
 
 
 
 
 
94
  elif isinstance(item, (list, tuple)) and len(item) == 2:
 
95
  user_msg, assistant_msg = item
 
96
  if user_msg:
97
- messages.append({"role": "user", "content": user_msg})
 
 
 
 
98
  if assistant_msg:
99
- messages.append({"role": "assistant", "content": assistant_msg})
 
 
 
100
 
101
- messages.append({"role": "user", "content": message})
 
 
 
 
102
 
103
  try:
 
104
  completion = client.chat.completions.create(
105
  model="Qwen/Qwen2.5-Coder-7B-Instruct",
106
  messages=messages,
@@ -108,16 +143,20 @@ def chat_function(message, history):
108
  temperature=0.7,
109
  top_p=0.95,
110
  )
 
111
  return completion.choices[0].message.content
 
112
  except Exception as e:
113
  return f"Error: {str(e)}"
114
 
 
115
  demo = gr.ChatInterface(
116
  fn=chat_function,
117
  type="messages",
118
  title="AI Coding Assistant",
119
- description="A coding assistant powered by Qwen2.5-Coder.",
120
  )
121
 
 
122
  if __name__ == "__main__":
123
  demo.launch()
 
63
  from dotenv import load_dotenv
64
  from huggingface_hub import InferenceClient
65
 
66
+ # Load environment variables
67
  load_dotenv()
68
 
69
  HF_TOKEN = os.getenv("HF_TOKEN")
70
+
71
  if not HF_TOKEN:
72
  raise ValueError("HF_TOKEN is missing.")
73
 
74
+ # Create HF client
75
+ client = InferenceClient(
76
+ provider="nscale",
77
+ api_key=HF_TOKEN
78
+ )
79
+
80
+ # System prompt
81
  system_message = (
82
  "You are a helpful and experienced coding assistant specialized in web development. "
83
  "Help the user by generating complete and functional code for building websites. "
84
+ "You can provide HTML, CSS, JavaScript, and backend code like Flask, Node.js, etc. "
85
  "based on their requirements."
86
  )
87
 
88
+ # Chat function
 
 
 
 
89
  def chat_function(message, history):
90
+
91
+ messages = [
92
+ {
93
+ "role": "system",
94
+ "content": system_message
95
+ }
96
+ ]
97
 
98
  history = history or []
99
+
100
  for item in history:
101
+
102
+ # Modern Gradio messages format
103
  if isinstance(item, dict):
104
+
105
  role = item.get("role")
106
  content = item.get("content", "")
107
+
108
+ if role in ["user", "assistant"]:
109
+ messages.append({
110
+ "role": role,
111
+ "content": content
112
+ })
113
+
114
+ # Older tuple fallback
115
  elif isinstance(item, (list, tuple)) and len(item) == 2:
116
+
117
  user_msg, assistant_msg = item
118
+
119
  if user_msg:
120
+ messages.append({
121
+ "role": "user",
122
+ "content": user_msg
123
+ })
124
+
125
  if assistant_msg:
126
+ messages.append({
127
+ "role": "assistant",
128
+ "content": assistant_msg
129
+ })
130
 
131
+ # Current user message
132
+ messages.append({
133
+ "role": "user",
134
+ "content": message
135
+ })
136
 
137
  try:
138
+
139
  completion = client.chat.completions.create(
140
  model="Qwen/Qwen2.5-Coder-7B-Instruct",
141
  messages=messages,
 
143
  temperature=0.7,
144
  top_p=0.95,
145
  )
146
+
147
  return completion.choices[0].message.content
148
+
149
  except Exception as e:
150
  return f"Error: {str(e)}"
151
 
152
+ # Interface
153
  demo = gr.ChatInterface(
154
  fn=chat_function,
155
  type="messages",
156
  title="AI Coding Assistant",
157
+ description="A coding assistant powered by Qwen2.5-Coder."
158
  )
159
 
160
+ # Launch
161
  if __name__ == "__main__":
162
  demo.launch()