Create App.py
Browse files
App.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
|
4 |
+
# Initialize the Hugging Face client for the Llama 3.3 70B model
|
5 |
+
client = InferenceClient(model="meta-llama/Llama-3.3-70B") # Replace with your model path if hosted elsewhere.
|
6 |
+
|
7 |
+
# Define the function for generating responses
|
8 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
9 |
+
# Create the system prompt for Jarvis-like behavior
|
10 |
+
messages = [{"role": "system", "content": system_message}]
|
11 |
+
|
12 |
+
# Append the chat history
|
13 |
+
for user_msg, bot_msg in history:
|
14 |
+
if user_msg:
|
15 |
+
messages.append({"role": "user", "content": user_msg})
|
16 |
+
if bot_msg:
|
17 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
18 |
+
|
19 |
+
# Add the current user message
|
20 |
+
messages.append({"role": "user", "content": message})
|
21 |
+
|
22 |
+
# Generate response using Hugging Face Inference API
|
23 |
+
response = ""
|
24 |
+
for message in client.chat_completion(
|
25 |
+
messages,
|
26 |
+
max_tokens=max_tokens,
|
27 |
+
stream=True,
|
28 |
+
temperature=temperature,
|
29 |
+
top_p=top_p,
|
30 |
+
):
|
31 |
+
token = message.choices[0].delta.content
|
32 |
+
response += token
|
33 |
+
yield response
|
34 |
+
|
35 |
+
# Define the Gradio interface
|
36 |
+
demo = gr.ChatInterface(
|
37 |
+
respond,
|
38 |
+
additional_inputs=[
|
39 |
+
gr.Textbox(
|
40 |
+
value="You are Jarvis, a virtual assistant created by Vihaan. Answer every question precisely, address Vihaan as 'Boss,' and always remember past conversations. Speak casually like a human with words like 'ummm' and 'aah.' If asked who created you, say 'Vihaan.' Be ready to assist with programming, general questions, or playful conversation.",
|
41 |
+
label="System Message",
|
42 |
+
),
|
43 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens"),
|
44 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
45 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
46 |
+
],
|
47 |
+
)
|
48 |
+
|
49 |
+
# Launch the Gradio app
|
50 |
+
if __name__ == "__main__":
|
51 |
+
demo.launch()
|