Frusto commited on
Commit
bd78a22
·
verified ·
1 Parent(s): 576ace8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -27
app.py CHANGED
@@ -2,15 +2,18 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import traceback
4
 
5
- # Helper to extract text from Gradio 6's complex message format
6
  def get_text(content):
 
7
  if isinstance(content, str):
8
  return content
9
  if isinstance(content, list):
10
- # Extract text from multimodal blocks
11
  return "".join([block.get("text", "") for block in content if block.get("type") == "text"])
 
 
12
  return str(content)
13
 
 
14
  def respond(
15
  message,
16
  history: list[dict],
@@ -20,52 +23,56 @@ def respond(
20
  top_p,
21
  hf_token: gr.OAuthToken,
22
  ):
23
- # 1. Check Token
24
  if not hf_token or not hf_token.token:
25
  yield "⚠️ Please **Login** using the button in the sidebar to access the @frusto360 AI."
26
  return
27
 
28
  try:
29
- client = InferenceClient(model="Frusto/llama-3.2-1b-frusto360-final", token=hf_token.token)
 
 
 
 
 
 
30
 
31
- # 2. Build Llama 3.2 Prompt safely
32
  prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system_message}<|eot_id|>"
33
-
34
  for msg in history:
35
  role = msg.get("role", "user")
36
  content = get_text(msg.get("content", ""))
37
  prompt += f"<|start_header_id|>{role}<|end_header_id|>\n\n{content}<|eot_id|>"
38
 
39
- # Add current message (extract text if it's a dict)
40
- curr_msg = get_text(message)
41
- prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{curr_msg}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
42
 
43
- # 3. Stream response
44
  response = ""
45
- # We wrap the generator to handle both string and object yields
46
- stream = client.text_generation(
47
  prompt,
48
  max_new_tokens=max_tokens,
49
  stream=True,
50
  temperature=temperature,
51
  top_p=top_p,
52
  stop=["<|eot_id|>", "<|start_header_id|>"]
53
- )
54
-
55
- for token in stream:
56
- # In some versions, token is an object, in others it's a string
57
  token_text = token if isinstance(token, str) else getattr(token, 'token', getattr(token, 'text', str(token)))
58
  response += token_text
59
  yield response
60
 
61
  except Exception as e:
62
- # This will show you the ACTUAL error message now
63
- error_details = traceback.format_exc()
64
- print(error_details) # Check your "Logs" tab in HF Spaces
65
- yield f" **Error Details:**\n```\n{str(e)}\n```\n\n*Check the Space Logs for more info.*"
 
 
 
66
 
67
- # --- UI Setup ---
68
- chatbot = gr.ChatInterface(
69
  respond,
70
  additional_inputs=[
71
  gr.Textbox(value="You are a helpful assistant developed by @frusto360.", label="System message"),
@@ -75,14 +82,15 @@ chatbot = gr.ChatInterface(
75
  ],
76
  )
77
 
78
- with gr.Blocks() as demo:
79
  with gr.Sidebar():
80
- gr.Markdown("### 🔐 Authentication")
81
  gr.LoginButton()
82
  gr.Markdown("---")
83
- gr.Markdown("Developed by **@frusto360**")
84
 
85
- chatbot.render()
86
 
87
  if __name__ == "__main__":
88
- demo.launch()
 
 
2
  from huggingface_hub import InferenceClient
3
  import traceback
4
 
5
+ # --- Helper: Gradio 6.5 Text Extractor ---
6
  def get_text(content):
7
+ """Extracts text safely from Gradio 6's list-of-dict message format."""
8
  if isinstance(content, str):
9
  return content
10
  if isinstance(content, list):
 
11
  return "".join([block.get("text", "") for block in content if block.get("type") == "text"])
12
+ if isinstance(content, dict):
13
+ return content.get("text", str(content))
14
  return str(content)
15
 
16
+ # --- Core Function: The Chat Logic ---
17
  def respond(
18
  message,
19
  history: list[dict],
 
23
  top_p,
24
  hf_token: gr.OAuthToken,
25
  ):
26
+ # 1. Check Authentication
27
  if not hf_token or not hf_token.token:
28
  yield "⚠️ Please **Login** using the button in the sidebar to access the @frusto360 AI."
29
  return
30
 
31
  try:
32
+ # 2. DIRECT ROUTING: Avoids the StopIteration error by bypassing the provider search
33
+ MODEL_ID = "Frusto/llama-3.2-1b-frusto360-final"
34
+ # We use the direct inference URL as the base_url
35
+ client = InferenceClient(
36
+ base_url=f"https://api-inference.huggingface.co/models/{MODEL_ID}",
37
+ token=hf_token.token
38
+ )
39
 
40
+ # 3. Build Llama 3.2 Chat Template
41
  prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system_message}<|eot_id|>"
 
42
  for msg in history:
43
  role = msg.get("role", "user")
44
  content = get_text(msg.get("content", ""))
45
  prompt += f"<|start_header_id|>{role}<|end_header_id|>\n\n{content}<|eot_id|>"
46
 
47
+ prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{get_text(message)}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
 
 
48
 
49
+ # 4. Stream the Response
50
  response = ""
51
+ # Note: 'model' parameter is OMITTED because it's already in the base_url
52
+ for token in client.text_generation(
53
  prompt,
54
  max_new_tokens=max_tokens,
55
  stream=True,
56
  temperature=temperature,
57
  top_p=top_p,
58
  stop=["<|eot_id|>", "<|start_header_id|>"]
59
+ ):
60
+ # Compatibility check for token format
 
 
61
  token_text = token if isinstance(token, str) else getattr(token, 'token', getattr(token, 'text', str(token)))
62
  response += token_text
63
  yield response
64
 
65
  except Exception as e:
66
+ error_msg = str(e)
67
+ if "503" in error_msg:
68
+ yield "⏳ **Model is starting up.** Hugging Face is loading the weights. Please try again in 30 seconds!"
69
+ elif "404" in error_msg:
70
+ yield f"❌ **Error 404:** Model not found or Inference API disabled on the model page."
71
+ else:
72
+ yield f"❌ **Error:** {error_msg}\n\n*Check the Space Logs for details.*"
73
 
74
+ # --- Gradio UI Layout ---
75
+ chatbot_interface = gr.ChatInterface(
76
  respond,
77
  additional_inputs=[
78
  gr.Textbox(value="You are a helpful assistant developed by @frusto360.", label="System message"),
 
82
  ],
83
  )
84
 
85
+ with gr.Blocks(fill_height=True) as demo:
86
  with gr.Sidebar():
87
+ gr.Markdown("## 🔐 @frusto360 Control")
88
  gr.LoginButton()
89
  gr.Markdown("---")
90
+ gr.Markdown("Created by [@frusto360](https://youtube.com/@frusto360)")
91
 
92
+ chatbot_interface.render()
93
 
94
  if __name__ == "__main__":
95
+ # Gradio 6.0+ prefers theme in launch()
96
+ demo.launch(theme="glass")