cetusian commited on
Commit
9c76c6f
·
verified ·
1 Parent(s): 9738654

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -54
app.py CHANGED
@@ -3,62 +3,50 @@ import os
3
  from huggingface_hub import login
4
  from transformers import pipeline
5
 
6
- # Here's my attempt at AI humor. Remember, I'm no comedian... or am I?
7
- def chat_with_llama(message, history):
8
- # Imagine if this function was actually calling a real llama for advice.
9
- # But no, it's just a model, much less spitting involved.
10
- generator = pipeline('text-generation', model='meta-llama/Llama-3.1-8B-Instruct')
11
- response = generator(message, max_length=150, num_return_sequences=1)
12
- return response[0]['generated_text']
13
 
14
- # Get the API key - if this fails, the app will just tell you to herd some real llamas instead.
15
- api_key = os.getenv("LLAMA") or "You forgot to set your environment variable. Go herd some real llamas!"
16
 
17
- # Logging in with a dramatic flair
18
- try:
19
- login(api_key)
20
- login_message = "Successfully logged in! Let the digital llama shenanigans begin!"
21
- except:
22
- login_message = "Login failed. Perhaps the llama is on a coffee break. Try again?"
23
 
24
- # UI Improvements with a touch of absurdity
25
- css = """
26
- body {background-color: #f0f8ff; font-family: 'Comic Sans MS', cursive, sans-serif;}
27
- .message {padding: 10px; margin: 5px; border-radius: 15px;}
28
- .user-message {background: #e6f3ff; color: #0000FF;}
29
- .llama-message {background: #ffe6e6; color: #FF0000; font-style: italic;}
30
- #chatbot {border: 5px solid #d3d3d3; border-radius: 20px; background: url('data:image/svg+xml;utf8,<svg ...>'); background-size: cover;}
31
- .button {background-color: #4CAF50; color: white; border: none; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; transition: all 0.3s ease;}
32
- .button:hover {background-color: #45a049; transform: scale(1.05);}
33
- """
34
-
35
- with gr.Blocks(css=css, theme=gr.themes.Monochrome()) as demo:
36
- gr.HTML("""
37
- <div style="text-align: center; background: #add8e6; padding: 10px;">
38
- <h1>🦙 Llama Chat 3000 - Where Wisdom Meets Wool</h1>
39
- <img src="llama_logo.png" alt="Llama Logo" width="100">
40
- </div>
41
- """)
42
 
43
- chatbot = gr.Chatbot(label="The Digital Llama", height=500)
44
- msg = gr.Textbox(placeholder="Type your question here or ask for a random fact about grass...")
45
- clear = gr.ClearButton([msg, chatbot])
46
-
47
- msg.submit(chat_with_llama, [msg, chatbot], chatbot)
 
 
 
48
 
49
- gr.HTML(f"<p style='text-align:center;'>{login_message}</p>")
50
-
51
- # A button for fun, because why not?
52
- def llama_joke():
53
- jokes = ["Why did the llama go to the party? Because he was the life of the alpaca!",
54
- "What do you call a very fast llama? A Llamaghini!"]
55
- return random.choice(jokes)
56
-
57
- gr.Button("Need a Llaugh?").click(llama_joke, [], chatbot)
58
-
59
- demo.title = "Llama Chat 3000"
60
- demo.description = "Engage in enlightening, or at least entertaining, conversation with an AI inspired by the majestic llama. No spitting, guaranteed!"
61
-
62
- # Instead of just launching, let's give it some flair
63
- print("Launching your very own digital petting zoo...")
64
- demo.queue().launch(share=True, debug=True)
 
 
 
 
 
 
 
3
  from huggingface_hub import login
4
  from transformers import pipeline
5
 
6
+ # Login using your Hugging Face API key
7
+ api_key = os.getenv("LLAMA")
8
+ login(api_key)
 
 
 
 
9
 
10
+ # Load the Llama model
11
+ llama_model = gr.load("models/meta-llama/Llama-3.1-8B-Instruct")
12
 
13
+ # Function to handle inputs and outputs
14
+ def chat_with_llama(user_input):
15
+ # Generate response using the pipeline
16
+ response = llama_model(user_input)
17
+ return response
 
18
 
19
+ # Customize Gradio interface
20
+ with gr.Blocks(css=".title {font-size: 3em; font-weight: bold; text-align: center; color: #4A90E2;}") as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ # Header section
23
+ gr.Markdown(
24
+ """
25
+ <div class="title">🦙 Llama 3.1 Chatbot 🦙</div>
26
+ <p style="text-align:center; font-size:1.2em; color:gray;">Ask anything from the advanced Llama 3.1 model!</p>
27
+ """,
28
+ elem_classes="header"
29
+ )
30
 
31
+ # Main input/output section
32
+ with gr.Row():
33
+ with gr.Column(scale=1):
34
+ user_input = gr.Textbox(label="Your question", placeholder="Type your question here...", lines=4)
35
+ with gr.Column(scale=1):
36
+ response_output = gr.Textbox(label="Llama's response", lines=4)
37
+
38
+ # Button to submit the input
39
+ submit_button = gr.Button("Submit", variant="primary")
40
+
41
+ # Link the input and output
42
+ submit_button.click(fn=chat_with_llama, inputs=user_input, outputs=response_output)
43
+
44
+ # Footer section
45
+ gr.Markdown(
46
+ """
47
+ <div style="text-align:center; font-size:0.8em; color:gray;">Developed with ❤️ using Llama 3.1 and Gradio</div>
48
+ """
49
+ )
50
+
51
+ # Launch the app
52
+ demo.launch()