Rob Learsch commited on
Commit
aea35bf
·
1 Parent(s): 28dca5a

Update app.py

Browse files

Barebones from CGPT

Files changed (1) hide show
  1. app.py +23 -62
app.py CHANGED
@@ -14,67 +14,28 @@ HF_API_KEY = os.environ["HF_API_KEY"]
14
  """
15
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
  """
17
- client = InferenceClient(
18
-
19
- provider="hf-inference",
20
- api_key=HF_API_KEY,
21
- )
22
-
23
- def respond(
24
- message,
25
- history,#: list[tuple[str, str]],
26
- system_message,
27
- max_tokens,
28
- temperature,
29
- top_p,
30
- ):
31
- #messages = [{"role": "system", "content": system_message}]
32
- messages=[]
33
- if history:
34
- for val in history:
35
- if val[0]:
36
- messages.append({"role": "user", "content": val[0]})
37
- if val[1]:
38
- messages.append({"role": "assistant", "content": val[1]})
39
-
40
- messages.append({"role": "user", "content": message})
41
-
42
- response = ""
43
-
44
- for message in client.chat_completion(
45
- messages,
46
- model='google/gemma-2-2b-it',
47
- max_tokens=512,
48
- stream=False,
49
- temperature=1,
50
- top_p=0.95,
51
- ):
52
- token = message.choices[0].delta.content
53
-
54
- response += token
55
- yield response
56
-
57
-
58
- """
59
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
60
- """
61
- demo = gr.ChatInterface(
62
- respond,
63
- additional_inputs=[
64
- gr.Textbox(value="You are a friendly ThomBot.", label="System message"),
65
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
66
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
67
- gr.Slider(
68
- minimum=0.1,
69
- maximum=1.0,
70
- value=0.95,
71
- step=0.05,
72
- label="Top-p (nucleus sampling)",
73
- ),
74
- ],
75
- type='messages'
76
- )
77
-
78
-
79
  if __name__ == "__main__":
80
  demo.launch()
 
14
  """
15
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
  """
17
+ # Initialize Hugging Face Inference Client
18
+ client = InferenceClient(model="google/gemma-2-2b-it", token=HF_API_KEY)
19
+
20
+ # Function to generate chatbot responses
21
+ def chat_with_gemma(user_input, history):
22
+ messages = [{"role": "user", "content": user_input}]
23
+
24
+ try:
25
+ response = client.chat_completion(
26
+ messages=messages,
27
+ model="google/gemma-2-2b-it",
28
+ max_tokens=256,
29
+ temperature=0.7,
30
+ top_p=0.9
31
+ )
32
+ return response["choices"][0]["message"]["content"]
33
+ except Exception as e:
34
+ return f"Error: {str(e)}"
35
+
36
+ # Gradio Chat Interface
37
+ demo = gr.ChatInterface(chat_with_gemma)
38
+
39
+ # Launch the chatbot
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  if __name__ == "__main__":
41
  demo.launch()