Walid-Ahmed commited on
Commit
ef6149e
·
verified ·
1 Parent(s): 1def611

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+ # Hugging Face API Token (from secrets)
6
+ API_TOKEN = os.getenv("HF_API_KEY")
7
+
8
+
9
+ # Model Endpoint
10
+ model = "mistralai/Mixtral-8x7B-Instruct-v0.1" # Use a working instruct model
11
+ api_url = f"https://api-inference.huggingface.co/models/{model}"
12
+
13
+ # Headers
14
+ headers = {"Authorization": f"Bearer {API_TOKEN}"}
15
+
16
+ # Function to generate email response
17
+ def generate_email_response(received_email, additional_context, tone="Professional and Polite"):
18
+ # Construct the prompt with additional context
19
+ prompt = f"""
20
+ The following is an email I received:
21
+
22
+ {received_email}
23
+
24
+ Additional context to consider:
25
+ {additional_context}
26
+
27
+ Please draft a response that is {tone}. Keep it concise and appropriate.
28
+
29
+ My response:
30
+ """
31
+
32
+ # API request payload
33
+ payload = {
34
+ "inputs": prompt,
35
+ "parameters": {"max_length": 400, "temperature": 0.7}
36
+ }
37
+
38
+ # Send request to HF API
39
+ response = requests.post(api_url, headers=headers, json=payload)
40
+
41
+ # Handle response
42
+ if response.status_code == 200:
43
+ generated_text = response.json()[0]["generated_text"]
44
+
45
+ # **Trim the response** to remove the echoed prompt
46
+ if "My response:" in generated_text:
47
+ generated_text = generated_text.split("My response:")[-1].strip()
48
+
49
+ return generated_text
50
+ else:
51
+ return "Error: " + response.json().get("error", "Unknown issue occurred.")
52
+
53
+
54
+ # Example email
55
+ example_email = """Subject: Request for Collaboration on AI Research
56
+
57
+ Dear Walid,
58
+
59
+ I hope you are doing well. My team and I have been following your recent work on hybrid neural networks and FLOPs-aware optimization. We are very interested in collaborating on a research project that explores the application of your techniques in low-resource environments.
60
+
61
+ Would you be available for a meeting next week to discuss potential collaboration opportunities? Please let us know your availability.
62
+
63
+ Looking forward to your response.
64
+
65
+ Best regards,
66
+ John Smith
67
+ AI Research Lead, Tech Innovations Inc."""
68
+
69
+ # Example additional context
70
+ example_context = """I am available on Monday, Wednesday, and Friday from 2 PM to 5 PM.
71
+ I prefer virtual meetings via Zoom or Google Meet."""
72
+
73
+ # Gradio Web Interface
74
+ with gr.Blocks() as demo:
75
+ gr.Markdown("## AI-Powered Email Responder (with Context)")
76
+ gr.Markdown("Enter an email, and the model will generate a response in your selected tone. You can also add additional context, such as your availability.")
77
+
78
+ with gr.Row():
79
+ received_email = gr.Textbox(label="Received Email", lines=10)
80
+ additional_context = gr.Textbox(label="Additional Context (Availability, Preferences, etc.)", lines=5)
81
+
82
+ tone = gr.Radio(
83
+ ["Professional and Polite", "Friendly and Casual", "Formal and Concise"],
84
+ label="Tone of Response",
85
+ value="Professional and Polite"
86
+ )
87
+
88
+ output_response = gr.Textbox(label="Generated Response", lines=6)
89
+
90
+ generate_button = gr.Button("Generate Response")
91
+ generate_button.click(generate_email_response, inputs=[received_email, additional_context, tone], outputs=output_response)
92
+
93
+ # Add example input as a predefined example
94
+ gr.Examples(
95
+ examples=[
96
+ [example_email, example_context, "Professional and Polite"]
97
+ ],
98
+ inputs=[received_email, additional_context, tone],
99
+ )
100
+
101
+ # Launch the Gradio app
102
+ demo.launch()
103
+