AiCoderv2 commited on
Commit
f13f1c5
·
verified ·
1 Parent(s): 9d46895

Update Gradio app with multiple files

Browse files
Files changed (2) hide show
  1. README.md +10 -11
  2. app.py +39 -22
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: AI Chatbot with Hugging Face Model
3
  emoji: 🤖
4
  colorFrom: blue
5
  colorTo: green
@@ -7,24 +7,23 @@ sdk: gradio
7
  sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
- tags:
11
- - anycoder
12
  ---
13
 
14
- # AI Chatbot with Hugging Face Model
15
 
16
- This is a simple chatbot application built with Gradio and powered by a Hugging Face conversational AI model (DialoGPT-medium).
17
 
18
  ## Features
19
 
20
- - Conversational AI using Microsoft's DialoGPT-medium model (larger and more capable than the small version)
 
21
  - Gradio interface for easy interaction
22
  - Maintains conversation history
23
- - Supports Hugging Face token for accessing private models or increased rate limits
24
 
25
  ## Setup
26
 
27
- To use a Hugging Face token (recommended for better performance and access to larger models):
28
 
29
  1. Create a Hugging Face account at https://huggingface.co
30
  2. Generate a token at https://huggingface.co/settings/tokens
@@ -34,13 +33,13 @@ To use a Hugging Face token (recommended for better performance and access to la
34
 
35
  1. Run the app: `python app.py`
36
  2. Open your browser to the provided URL
37
- 3. Start chatting with the AI!
38
 
39
  ## Model
40
 
41
- The app uses `microsoft/DialoGPT-medium`, a conversational model trained on Reddit conversations. This is a larger model than the previous small version, offering better responses but requiring more computational resources.
42
 
43
- Note: Larger models like this may take longer to load and respond, especially on free-tier hosting.
44
 
45
  ## Built with anycoder
46
 
 
1
  ---
2
+ title: Coding Expert AI Chatbot
3
  emoji: 🤖
4
  colorFrom: blue
5
  colorTo: green
 
7
  sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
 
 
10
  ---
11
 
12
+ # Coding Expert AI Chatbot
13
 
14
+ This is a chatbot application built with Gradio and powered by Microsoft's Phi-2 model, which is specialized in coding and general conversational tasks.
15
 
16
  ## Features
17
 
18
+ - AI powered by Phi-2 (2.7B parameters), excellent for coding assistance
19
+ - Streaming responses for faster interaction (text appears as it's generated)
20
  - Gradio interface for easy interaction
21
  - Maintains conversation history
22
+ - Supports Hugging Face token for accessing models
23
 
24
  ## Setup
25
 
26
+ To use a Hugging Face token (recommended):
27
 
28
  1. Create a Hugging Face account at https://huggingface.co
29
  2. Generate a token at https://huggingface.co/settings/tokens
 
33
 
34
  1. Run the app: `python app.py`
35
  2. Open your browser to the provided URL
36
+ 3. Start chatting with the AI! It can help with coding questions and general conversations.
37
 
38
  ## Model
39
 
40
+ The app uses `microsoft/phi-2`, a 2.7B parameter model fine-tuned for coding and instruction-following tasks. It's larger than previous models and provides better responses, especially for technical questions.
41
 
42
+ Note: This model may take longer to load and respond on free-tier hosting, but responses stream in real-time.
43
 
44
  ## Built with anycoder
45
 
app.py CHANGED
@@ -1,41 +1,58 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  import os
 
4
 
5
- # Load the conversational model with HF token support
6
- # Using DialoGPT-medium for a larger, more capable chatbot
7
  token = os.getenv('HF_TOKEN')
8
- chatbot_model = pipeline("text-generation", model="microsoft/DialoGPT-medium", token=token)
 
 
9
 
10
  def chat(message, history):
11
- # Build conversation string from history
12
- conversation_text = ""
13
  for user_msg, bot_msg in history:
14
  if user_msg:
15
- conversation_text += f"@@PADDING@@ {user_msg} @@PADDING@@ "
16
  if bot_msg:
17
- conversation_text += f"{bot_msg} @@PADDING@@ "
18
 
19
- # Add current user message
20
- conversation_text += f"@@PADDING@@ {message} @@PADDING@@ "
21
 
22
- # Generate response
23
- result = chatbot_model(conversation_text, max_length=1000, num_return_sequences=1, temperature=0.8, do_sample=True, pad_token_id=50256)
24
 
25
- # Extract the response
26
- generated_text = result[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- # Split by padding and take the last part as response
29
- parts = generated_text.split("@@PADDING@@")
30
- response = parts[-1].strip() if parts else generated_text.strip()
31
-
32
- return response
33
 
34
- # Create Gradio interface
35
  demo = gr.ChatInterface(
36
  fn=chat,
37
- title="AI Chatbot",
38
- description="Chat with an AI powered by Hugging Face transformers. <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>Built with anycoder</a>",
39
  theme=gr.themes.Soft()
40
  )
41
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  import os
4
+ import torch
5
 
6
+ # Load the model and tokenizer for a coding expert AI
7
+ # Using Phi-2 which is good for coding and conversational tasks
8
  token = os.getenv('HF_TOKEN')
9
+ model_name = "microsoft/phi-2"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name, token=token, trust_remote_code=True)
11
+ model = AutoModelForCausalLM.from_pretrained(model_name, token=token, trust_remote_code=True, torch_dtype=torch.float16, device_map="auto")
12
 
13
  def chat(message, history):
14
+ # Build conversation prompt
15
+ prompt = ""
16
  for user_msg, bot_msg in history:
17
  if user_msg:
18
+ prompt += f"User: {user_msg}\n"
19
  if bot_msg:
20
+ prompt += f"Assistant: {bot_msg}\n"
21
 
22
+ prompt += f"User: {message}\nAssistant:"
 
23
 
24
+ # Tokenize input
25
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
26
 
27
+ # Generate response with streaming
28
+ generated_tokens = []
29
+ with torch.no_grad():
30
+ for _ in range(100): # Limit to prevent infinite generation
31
+ outputs = model(**inputs)
32
+ next_token_logits = outputs.logits[:, -1, :]
33
+ next_token = torch.multinomial(torch.softmax(next_token_logits, dim=-1), num_samples=1)
34
+ generated_tokens.append(next_token.item())
35
+
36
+ # Yield partial response
37
+ current_text = tokenizer.decode(generated_tokens, skip_special_tokens=True)
38
+ yield current_text
39
+
40
+ # Check for end of response (simple heuristic: if ends with newline or period)
41
+ if current_text.endswith(('\n', '.', '!', '?')) and len(current_text) > 10:
42
+ break
43
+
44
+ # Update inputs for next token
45
+ inputs = torch.cat([inputs['input_ids'], next_token], dim=-1)
46
 
47
+ # Final yield
48
+ final_response = tokenizer.decode(generated_tokens, skip_special_tokens=True).strip()
49
+ yield final_response
 
 
50
 
51
+ # Create Gradio interface with streaming enabled
52
  demo = gr.ChatInterface(
53
  fn=chat,
54
+ title="Coding Expert AI Chatbot",
55
+ description="Chat with a coding expert AI powered by Phi-2. It can help with programming questions and general conversations. <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>Built with anycoder</a>",
56
  theme=gr.themes.Soft()
57
  )
58