Facececersek commited on
Commit
e3f6097
·
verified ·
1 Parent(s): 2807873

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +38 -49
app.py CHANGED
@@ -1,67 +1,50 @@
1
  import gradio as gr
2
- from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
 
5
- # Model: Mistral 7B Instruct - powerful open model
6
- MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
 
7
 
8
- print(f"Loading model: {MODEL_NAME}...")
9
-
10
- # Initialize text generation pipeline
11
- try:
12
- generator = pipeline(
13
- "text-generation",
14
- model=MODEL_NAME,
15
- torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
16
- device_map="auto" if torch.cuda.is_available() else None,
17
- trust_remote_code=True
18
- )
19
- print("Model loaded successfully!")
20
- except Exception as e:
21
- print(f"Error loading model: {e}")
22
- # Fallback to a smaller model
23
- MODEL_NAME = "microsoft/DialoGPT-medium"
24
- generator = pipeline("text-generation", model=MODEL_NAME)
25
- print(f"Loaded fallback model: {MODEL_NAME}")
26
 
27
  def chat_with_ai(message, history):
28
- """Chat with the AI model."""
29
  if not message.strip():
30
  return history
31
 
32
- # Build conversation prompt
33
- conversation = ""
34
  for user_msg, assistant_msg in history:
35
- conversation += f"<|user|>\n{user_msg}</s>\n<|assistant|\n{assistant_msg}</s>\n"
36
- conversation += f"<|user|>\n{message}</s>\n<|assistant|"
 
37
 
38
  try:
39
- # Generate response
40
- response = generator(
41
- conversation,
42
- max_new_tokens=512,
 
43
  temperature=0.7,
44
- top_p=0.9,
45
- do_sample=True,
46
- pad_token_id=generator.tokenizer.eos_token_id
47
  )
48
 
49
- # Extract only the new response
50
- full_text = response[0]['generated_text']
51
- # Get the part after the last assistant tag
52
- if "<|assistant| " in full_text:
53
- assistant_response = full_text.split("<|assistant|")[-1].strip()
54
- else:
55
- assistant_response = full_text[len(conversation):].strip()
56
-
57
- # Clean up any remaining tags
58
- assistant_response = assistant_response.replace("</s>", "").strip()
59
 
60
- if not assistant_response:
61
- assistant_response = "I'm thinking... could you ask that again?"
62
-
63
  except Exception as e:
64
- assistant_response = f"Sorry, I encountered an error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  history.append((message, assistant_response))
67
  return history
@@ -78,10 +61,13 @@ with gr.Blocks(
78
  max-width: 800px !important;
79
  margin: auto !important;
80
  }
 
 
 
81
  """
82
  ) as demo:
83
  gr.Markdown("# 🤖 AI Chat Assistant")
84
- gr.Markdown(f"Powered by **{MODEL_NAME}**")
85
 
86
  chatbot = gr.Chatbot(
87
  label="Chat",
@@ -103,6 +89,9 @@ with gr.Blocks(
103
  with gr.Row():
104
  clear_btn = gr.Button("Clear Chat", variant="secondary")
105
 
 
 
 
106
  # Event handlers
107
  msg.submit(chat_with_ai, [msg, chatbot], [chatbot]).then(
108
  lambda: "", None, [msg]
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import os
4
 
5
+ # Use Inference API - no need to load model locally
6
+ MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.3"
7
+ client = InferenceClient(token=os.environ.get("HF_TOKEN"))
8
 
9
+ print(f"Using model: {MODEL_NAME} via Inference API")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def chat_with_ai(message, history):
12
+ """Chat with the AI model via Inference API."""
13
  if not message.strip():
14
  return history
15
 
16
+ # Convert history to messages format
17
+ messages = []
18
  for user_msg, assistant_msg in history:
19
+ messages.append({"role": "user", "content": user_msg})
20
+ messages.append({"role": "assistant", "content": assistant_msg})
21
+ messages.append({"role": "user", "content": message})
22
 
23
  try:
24
+ # Call the Inference API
25
+ response = client.chat.completions.create(
26
+ model=MODEL_NAME,
27
+ messages=messages,
28
+ max_tokens=512,
29
  temperature=0.7,
30
+ top_p=0.9
 
 
31
  )
32
 
33
+ assistant_response = response.choices[0].message.content
 
 
 
 
 
 
 
 
 
34
 
 
 
 
35
  except Exception as e:
36
+ # Fallback to text generation if chat fails
37
+ try:
38
+ prompt = f"User: {message}\nAssistant:"
39
+ response = client.text_generation(
40
+ model=MODEL_NAME,
41
+ prompt=prompt,
42
+ max_new_tokens=256,
43
+ temperature=0.7
44
+ )
45
+ assistant_response = response
46
+ except Exception as e2:
47
+ assistant_response = f"Sorry, couldn't connect to the model. Error: {str(e)}"
48
 
49
  history.append((message, assistant_response))
50
  return history
 
61
  max-width: 800px !important;
62
  margin: auto !important;
63
  }
64
+ footer {
65
+ display: none !important;
66
+ }
67
  """
68
  ) as demo:
69
  gr.Markdown("# 🤖 AI Chat Assistant")
70
+ gr.Markdown(f"Powered by **Mistral-7B-Instruct**")
71
 
72
  chatbot = gr.Chatbot(
73
  label="Chat",
 
89
  with gr.Row():
90
  clear_btn = gr.Button("Clear Chat", variant="secondary")
91
 
92
+ gr.Markdown("---")
93
+ gr.Markdown("*Space made by: you can already see it*")
94
+
95
  # Event handlers
96
  msg.submit(chat_with_ai, [msg, chatbot], [chatbot]).then(
97
  lambda: "", None, [msg]