Bhaskar2611 commited on
Commit
92f9195
·
verified ·
1 Parent(s): 57ba5b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -57
app.py CHANGED
@@ -63,105 +63,61 @@ import gradio as gr
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
- # Initialize client
75
- client = InferenceClient(
76
- api_key=HF_TOKEN,
77
- )
78
-
79
- # System prompt
80
  system_message = (
81
  "You are a helpful and experienced coding assistant specialized in web development. "
82
  "Help the user by generating complete and functional code for building websites. "
83
- "You can provide HTML, CSS, JavaScript, and backend code like Flask, Node.js, etc. "
84
  "based on their requirements."
85
  )
86
 
87
- # Chat function
88
- def chat_function(message, history):
 
 
89
 
90
- messages = [
91
- {
92
- "role": "system",
93
- "content": system_message
94
- }
95
- ]
96
 
97
  history = history or []
98
-
99
- # Handle Gradio history properly
100
  for item in history:
101
-
102
- # 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
- # 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
- # Add latest 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:nscale",
141
  messages=messages,
142
  max_tokens=2048,
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
- # UI
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
- examples=[
159
- "Create a responsive landing page.",
160
- "Build a Flask authentication API.",
161
- "Make a Node.js CRUD app."
162
- ]
163
  )
164
 
165
- # Launch
166
  if __name__ == "__main__":
167
  demo.launch()
 
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,
107
  max_tokens=2048,
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()