ariankhalfani commited on
Commit
47fd10e
·
verified ·
1 Parent(s): 6ad7be6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import streamlit as st
4
+
5
+ # Get the Hugging Face API Token from environment variables
6
+ HF_API_TOKEN = os.getenv("HF_API_KEY")
7
+ if not HF_API_TOKEN:
8
+ raise ValueError("Hugging Face API Token is not set in the environment variables.")
9
+
10
+ # Hugging Face API URLs and headers for Gemma models
11
+ GEMMA_7B_API_URL = "https://api-inference.huggingface.co/models/google/gemma-1.1-7b-it"
12
+ GEMMA_27B_API_URL = "https://api-inference.huggingface.co/models/google/gemma-2-27b-it"
13
+
14
+ HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"}
15
+
16
+ def query_model(api_url, payload):
17
+ response = requests.post(api_url, headers=HEADERS, json=payload)
18
+ return response.json()
19
+
20
+ def add_message_to_conversation(user_message, bot_message, model_name):
21
+ st.session_state.conversation.append((user_message, bot_message, model_name))
22
+
23
+ # Streamlit app
24
+ st.set_page_config(page_title="Gemma Chatbot Interface", layout="wide")
25
+ st.title("Gemma Chatbot Interface")
26
+ st.write("Gemma Chatbot Interface")
27
+
28
+ # Initialize session state for conversation and model history
29
+ if "conversation" not in st.session_state:
30
+ st.session_state.conversation = []
31
+ if "model_history" not in st.session_state:
32
+ st.session_state.model_history = {model: [] for model in ["Gemma-1.1-7B", "Gemma-2-27B"]}
33
+
34
+ # Dropdown for Gemma model selection
35
+ gemma_selection = st.selectbox("Select Gemma Model", ["Gemma-1.1-7B", "Gemma-2-27B"])
36
+
37
+ # User input for question
38
+ question = st.text_input("Question", placeholder="Enter your question here...")
39
+
40
+ # Handle user input and Gemma model response
41
+ if st.button("Send") and question:
42
+ try:
43
+ with st.spinner("Waiting for the model to respond..."):
44
+ chat_history = " ".join(st.session_state.model_history[gemma_selection]) + f"User: {question}\n"
45
+ if gemma_selection == "Gemma-1.1-7B":
46
+ response = query_model(GEMMA_7B_API_URL, {"inputs": chat_history})
47
+ elif gemma_selection == "Gemma-2-27B":
48
+ response = query_model(GEMMA_27B_API_URL, {"inputs": chat_history})
49
+
50
+ answer = response.get("generated_text", "No response") if isinstance(response, dict) else response[0].get("generated_text", "No response") if isinstance(response, list) else "No response"
51
+ add_message_to_conversation(question, answer, gemma_selection)
52
+ st.session_state.model_history[gemma_selection].append(f"User: {question}\n{gemma_selection}: {answer}\n")
53
+ except ValueError as e:
54
+ st.error(str(e))
55
+
56
+ # Custom CSS for chat bubbles
57
+ st.markdown(
58
+ """
59
+ <style>
60
+ .chat-bubble {
61
+ padding: 10px 14px;
62
+ border-radius: 14px;
63
+ margin-bottom: 10px;
64
+ display: inline-block;
65
+ max-width: 80%;
66
+ color: black;
67
+ }
68
+ .chat-bubble.user {
69
+ background-color: #dcf8c6;
70
+ align-self: flex-end;
71
+ }
72
+ .chat-bubble.bot {
73
+ background-color: #fff;
74
+ align-self: flex-start;
75
+ }
76
+ .chat-container {
77
+ display: flex;
78
+ flex-direction: column;
79
+ gap: 10px;
80
+ margin-top: 20px;
81
+ }
82
+ </style>
83
+ """,
84
+ unsafe_allow_html=True
85
+ )
86
+
87
+ # Display the conversation
88
+ st.write('<div class="chat-container">', unsafe_allow_html=True)
89
+ for user_message, bot_message, model_name in st.session_state.conversation:
90
+ st.write(f'<div class="chat-bubble user">You: {user_message}</div>', unsafe_allow_html=True)
91
+ st.write(f'<div class="chat-bubble bot">{model_name}: {bot_message}</div>', unsafe_allow_html=True)
92
+ st.write('</div>', unsafe_allow_html=True)