Sarath0x8f commited on
Commit
22faf28
·
verified ·
1 Parent(s): a45800b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py CHANGED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
2
+ import gradio as gr
3
+ import base64
4
+ import datetime
5
+
6
+ client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
7
+
8
+ # Debate response function
9
+ def debate_respond(message, history: list[tuple[str, str]],
10
+ max_tokens=1024, temperature=0.4, top_p=0.95):
11
+ # System message defining assistant behavior in a debate
12
+ system_message = {
13
+ "role": "system",
14
+ "content": f"Act as a debate participant taking the position '{position}' on the topic '{topic}'. Respond professionally, thoughtfully, and convincingly, staying within the specified role."
15
+ f"If the user's point challenges your position, provide a counterargument. Maintain a respectful tone throughout the discussion."
16
+ }
17
+
18
+ messages = [system_message]
19
+
20
+ # Adding conversation history
21
+ for val in history:
22
+ if val[0]:
23
+ messages.append({"role": "user", "content": val[0]})
24
+ if val[1]:
25
+ messages.append({"role": "assistant", "content": val[1]})
26
+
27
+ # Adding the current user input
28
+ messages.append({"role": "user", "content": message})
29
+
30
+ # Generating the response
31
+ response = ""
32
+ for message in client.chat_completion(
33
+ messages,
34
+ max_tokens=max_tokens,
35
+ stream=True,
36
+ temperature=temperature,
37
+ top_p=top_p,
38
+ ):
39
+ response += message.choices[0].delta.content
40
+ yield response
41
+ print(f"{datetime.datetime.now()}::{messages[-1]['content']}->{response}\n")
42
+
43
+ # Encode image function for logos (optional, kept for design)
44
+ def encode_image(image_path):
45
+ with open(image_path, "rb") as image_file:
46
+ return base64.b64encode(image_file.read()).decode('utf-8')
47
+
48
+ # Gradio interface
49
+ global topic, position
50
+ with gr.Blocks(theme=gr.themes.Ocean(font=[gr.themes.GoogleFont("Roboto Mono")]),
51
+ css='footer {visibility: hidden}') as demo:
52
+ gr.Markdown("# LLM Debate Participant")
53
+ with gr.Tabs():
54
+ with gr.TabItem("Debate Interface"):
55
+ with gr.Row():
56
+ topic = gr.Textbox(label="Debate Topic", placeholder="Enter the topic of the debate")
57
+ position = gr.Radio(["For", "Against"], label="Position", info="LLM's debate stance")
58
+ chatbot = gr.Chatbot(height=500)
59
+ debate_interface = gr.ChatInterface(debate_respond,
60
+ chatbot=chatbot,
61
+ examples=[
62
+ "Why do you support this stance?",
63
+ "Can you refute the opposing view on this topic?",
64
+ "What evidence supports your position?"
65
+ ]
66
+ )
67
+ gr.HTML("<footer><p>LLM Debate Participant © 2024</p></footer>")
68
+
69
+ if __name__ == "__main__":
70
+ demo.launch(share=True)