Spaces:
Sleeping
Sleeping
Create prompt_modals.py
Browse files- prompt_modals.py +50 -0
prompt_modals.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import InferenceClient
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
api_key = os.getenv('ImagiGen_HF_secret')
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def clean_generated_text(text):
|
| 8 |
+
# Remove asterisks (e.g., **text** or *text*)
|
| 9 |
+
text = re.sub(r"\*+", "", text)
|
| 10 |
+
|
| 11 |
+
# Remove special characters (except common punctuation and alphanumeric)
|
| 12 |
+
text = re.sub(r'[^a-zA-Z0-9 .,!?\'"-]', "", text)
|
| 13 |
+
|
| 14 |
+
# Normalize multiple spaces into a single space
|
| 15 |
+
text = re.sub(r"\s+", " ", text).strip()
|
| 16 |
+
return text
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def generate_prompt_response(api_key, model_name, user_message, max_tokens=1000):
|
| 20 |
+
client = InferenceClient(api_key=api_key)
|
| 21 |
+
messages = [{"role": "user", "content": user_message}]
|
| 22 |
+
|
| 23 |
+
# Generate the completion response
|
| 24 |
+
stream = client.chat.completions.create(
|
| 25 |
+
model=model_name, messages=messages, max_tokens=max_tokens, stream=True
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# Collect the response
|
| 29 |
+
response = ""
|
| 30 |
+
for chunk in stream:
|
| 31 |
+
response += chunk.choices[0].delta.content
|
| 32 |
+
return clean_generated_text(response)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def Qwen_72b(user_input):
|
| 36 |
+
model_name = "Qwen/Qwen2.5-72B-Instruct"
|
| 37 |
+
response = generate_prompt_response(api_key, model_name, user_message=user_input)
|
| 38 |
+
return clean_generated_text(response)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def Mixtral(user_input):
|
| 42 |
+
model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 43 |
+
response = generate_prompt_response(api_key, model_name, user_message=user_input)
|
| 44 |
+
return clean_generated_text(response)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def microsoft_phi(user_input):
|
| 48 |
+
model_name = "microsoft/Phi-3-mini-4k-instruct"
|
| 49 |
+
response = generate_prompt_response(api_key, model_name, user_message=user_input)
|
| 50 |
+
return clean_generated_text(response)
|