Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,69 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# Title and description
|
| 6 |
title = "π§ Claude-3.7-Sonnet-Reasoning-Gemma3-12B Chat"
|
| 7 |
description = """
|
| 8 |
-
Chat with the Claude-3.7-Sonnet-Reasoning-Gemma3-12B model
|
| 9 |
"""
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"reedmayhew/claude-3.7-sonnet-reasoning-gemma3-12B",
|
| 17 |
-
torch_dtype=torch.float16,
|
| 18 |
-
device_map="auto",
|
| 19 |
-
low_cpu_mem_usage=True
|
| 20 |
-
)
|
| 21 |
-
return model, tokenizer
|
| 22 |
except Exception as e:
|
| 23 |
-
|
| 24 |
-
return None, None
|
| 25 |
-
|
| 26 |
-
model, tokenizer = load_model()
|
| 27 |
|
| 28 |
def chat_with_claude(message, chat_history):
|
| 29 |
-
"""Chat function
|
| 30 |
-
if model is None or tokenizer is None:
|
| 31 |
-
return "β Model not loaded. Please check the logs for errors.", chat_history
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
conversation += f"Human: {
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# Extract
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
def clear_chat():
|
| 73 |
"""Clear the chat history"""
|
|
@@ -78,9 +74,17 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 78 |
gr.Markdown(f"# {title}")
|
| 79 |
gr.Markdown(description)
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
chatbot = gr.Chatbot(
|
| 82 |
label="Chat with Claude",
|
| 83 |
-
height=500
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
with gr.Row():
|
|
@@ -88,7 +92,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 88 |
label="Type your message here...",
|
| 89 |
placeholder="Ask me anything...",
|
| 90 |
lines=2,
|
| 91 |
-
scale=4
|
|
|
|
| 92 |
)
|
| 93 |
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 94 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
# Title and description
|
| 6 |
title = "π§ Claude-3.7-Sonnet-Reasoning-Gemma3-12B Chat"
|
| 7 |
description = """
|
| 8 |
+
Chat with the Claude-3.7-Sonnet-Reasoning-Gemma3-12B model using Hugging Face Inference API.
|
| 9 |
"""
|
| 10 |
|
| 11 |
+
# Hugging Face API setup
|
| 12 |
+
API_URL = "https://api-inference.huggingface.co/models/reedmayhew/claude-3.7-sonnet-reasoning-gemma3-12B"
|
| 13 |
+
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN', '')}"}
|
| 14 |
+
|
| 15 |
+
def query_hf_api(payload):
|
| 16 |
+
"""Query Hugging Face Inference API"""
|
| 17 |
try:
|
| 18 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 19 |
+
return response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def chat_with_claude(message, chat_history):
|
| 24 |
+
"""Chat function using HF Inference API"""
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Build conversation context
|
| 27 |
+
conversation = ""
|
| 28 |
+
for msg in chat_history:
|
| 29 |
+
if msg["role"] == "user":
|
| 30 |
+
conversation += f"Human: {msg['content']}\n"
|
| 31 |
+
else:
|
| 32 |
+
conversation += f"Assistant: {msg['content']}\n"
|
| 33 |
+
|
| 34 |
+
conversation += f"Human: {message}\nAssistant:"
|
| 35 |
+
|
| 36 |
+
# Call the API
|
| 37 |
+
payload = {
|
| 38 |
+
"inputs": conversation,
|
| 39 |
+
"parameters": {
|
| 40 |
+
"max_new_tokens": 500,
|
| 41 |
+
"temperature": 0.7,
|
| 42 |
+
"top_p": 0.9,
|
| 43 |
+
"do_sample": True
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
response = query_hf_api(payload)
|
| 48 |
+
|
| 49 |
+
if "error" in response:
|
| 50 |
+
assistant_response = f"β API Error: {response['error']}"
|
| 51 |
+
elif isinstance(response, list) and len(response) > 0:
|
| 52 |
+
# Extract the generated text
|
| 53 |
+
if "generated_text" in response[0]:
|
| 54 |
+
full_text = response[0]["generated_text"]
|
| 55 |
+
# Extract only the assistant's response
|
| 56 |
+
assistant_response = full_text.split("Assistant:")[-1].strip()
|
| 57 |
+
else:
|
| 58 |
+
assistant_response = "β Unexpected response format from API"
|
| 59 |
+
else:
|
| 60 |
+
assistant_response = "β No response from API"
|
| 61 |
+
|
| 62 |
+
# Update chat history
|
| 63 |
+
chat_history.append({"role": "user", "content": message})
|
| 64 |
+
chat_history.append({"role": "assistant", "content": assistant_response})
|
| 65 |
+
|
| 66 |
+
return "", chat_history
|
| 67 |
|
| 68 |
def clear_chat():
|
| 69 |
"""Clear the chat history"""
|
|
|
|
| 74 |
gr.Markdown(f"# {title}")
|
| 75 |
gr.Markdown(description)
|
| 76 |
|
| 77 |
+
# Add HF Token info
|
| 78 |
+
gr.Markdown("""
|
| 79 |
+
**Note:** To use this chat, you need to add your Hugging Face token in the Space settings:
|
| 80 |
+
1. Go to Settings β Repository secrets
|
| 81 |
+
2. Add `HF_TOKEN` with your Hugging Face token
|
| 82 |
+
""")
|
| 83 |
+
|
| 84 |
chatbot = gr.Chatbot(
|
| 85 |
label="Chat with Claude",
|
| 86 |
+
height=500,
|
| 87 |
+
type="messages" # Fixed the deprecation warning
|
| 88 |
)
|
| 89 |
|
| 90 |
with gr.Row():
|
|
|
|
| 92 |
label="Type your message here...",
|
| 93 |
placeholder="Ask me anything...",
|
| 94 |
lines=2,
|
| 95 |
+
scale=4,
|
| 96 |
+
container=False
|
| 97 |
)
|
| 98 |
submit_btn = gr.Button("Send", variant="primary", scale=1)
|
| 99 |
|