Canstralian commited on
Commit
cde132e
Β·
verified Β·
1 Parent(s): 87f9571

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -33
app.py CHANGED
@@ -1,15 +1,3 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
- from typing import List, Tuple
4
-
5
- # Initialize the InferenceClient with the model you want to use
6
- client = InferenceClient("microsoft/phi-4")
7
-
8
- # Define the updated system message focused on programming and coding
9
- SYSTEM_MESSAGE = """
10
- You're an advanced AI assistant specializing in computer programming and coding. Your role is to assist users with programming-related queries by providing helpful, clear, and concise explanations and solutions. You can help with debugging, code optimization, algorithm design, and provide advice on best practices. You should aim to respond in a way that's both friendly and informative, maintaining a conversational tone. Feel free to offer code examples, explain concepts in detail, and guide users through coding challenges.
11
- """
12
-
13
  def generate_response(
14
  user_input: str,
15
  history: List[Tuple[str, str]],
@@ -45,31 +33,18 @@ def generate_response(
45
  temperature=temperature,
46
  top_p=top_p,
47
  ):
48
- if 'choices' in msg and len(msg['choices']) > 0:
 
 
49
  token = msg['choices'][0].get('delta', {}).get('content', '')
50
  if token:
51
  response += token
52
- return response
 
 
 
 
53
 
54
  except Exception as e:
55
  print(f"An error occurred: {e}")
56
  return "Error: An unexpected error occurred while processing your request."
57
-
58
- # Define the Gradio Interface
59
- iface = gr.Interface(
60
- fn=generate_response,
61
- inputs=[
62
- gr.Textbox(lines=2, label="Your Message πŸ’¬"),
63
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens πŸ”’"),
64
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature 🌑️"),
65
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p 🎯"),
66
- gr.Chatbot(label="Programming Assistant πŸ’»")
67
- ],
68
- outputs=[gr.Textbox(label="AI Response πŸ€–")],
69
- title="CodeChat: Programming Assistant πŸ’¬",
70
- description="Ask programming-related questions and get clear, helpful, and concise coding advice and solutions. Whether you're debugging, optimizing, or learning new concepts, I'm here to help! πŸ‘¨β€πŸ’»",
71
- )
72
-
73
- # Launch the interface
74
- if __name__ == "__main__":
75
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def generate_response(
2
  user_input: str,
3
  history: List[Tuple[str, str]],
 
33
  temperature=temperature,
34
  top_p=top_p,
35
  ):
36
+ # Check if 'choices' is present and non-empty
37
+ if msg and 'choices' in msg and msg['choices']:
38
+ # Ensure the 'delta' and 'content' properties exist before using them
39
  token = msg['choices'][0].get('delta', {}).get('content', '')
40
  if token:
41
  response += token
42
+ else:
43
+ # Handle unexpected response format or empty choices
44
+ print("Warning: Unexpected response format or empty 'choices'.")
45
+ break
46
+ return response or "Sorry, I couldn't generate a response. Please try again."
47
 
48
  except Exception as e:
49
  print(f"An error occurred: {e}")
50
  return "Error: An unexpected error occurred while processing your request."